From 6e8c44f7741fef6ce1441633457dc5b1a3fb8c20 Mon Sep 17 00:00:00 2001 From: Vladimir Matveev Date: Fri, 10 Mar 2017 10:33:09 -0800 Subject: [PATCH] Ports #14566 to release-2.2 (#14571) * use ES6 library when building tslint rules (#14474) * Merge pull request #14553 from Microsoft/fixBuildBreak Add --lib es6 to @types/node dependent targets * allow passing --logFile and --logVerbosity parameter to tsserver (#14566) * fix linter issues --- Jakefile.js | 24 ++++--- scripts/parallel-lint.js | 3 +- src/harness/tsconfig.json | 4 ++ src/server/builder.ts | 1 - src/server/cancellationToken/tsconfig.json | 3 + src/server/server.ts | 65 +++++++++++-------- src/server/tsconfig.library.json | 3 +- .../typingsInstaller/nodeTypingsInstaller.ts | 10 ++- src/server/typingsInstaller/tsconfig.json | 4 ++ src/server/watchGuard/tsconfig.json | 10 ++- 10 files changed, 84 insertions(+), 43 deletions(-) diff --git a/Jakefile.js b/Jakefile.js index 4ef576f6675..77f1bef81d9 100644 --- a/Jakefile.js +++ b/Jakefile.js @@ -317,8 +317,14 @@ function compileFile(outFile, sources, prereqs, prefixes, useBuiltCompiler, opts if (opts.stripInternal) { options += " --stripInternal"; } - - options += " --target es5 --lib es5,scripthost --noUnusedLocals --noUnusedParameters"; + options += " --target es5"; + if (opts.lib) { + options += " --lib " + opts.lib + } + else { + options += " --lib es5,scripthost" + } + options += " --noUnusedLocals --noUnusedParameters"; var cmd = host + " " + compilerPath + " " + options + " "; cmd = cmd + sources.join(" "); @@ -405,7 +411,7 @@ compileFile(buildProtocolJs, [buildProtocolTs], [], /*useBuiltCompiler*/ false, - {noOutFile: true}); + { noOutFile: true, lib: "es6" }); file(buildProtocolDts, [buildProtocolTs, buildProtocolJs, typescriptServicesDts], function() { @@ -567,16 +573,16 @@ compileFile( file(typescriptServicesDts, [servicesFile]); var cancellationTokenFile = path.join(builtLocalDirectory, "cancellationToken.js"); -compileFile(cancellationTokenFile, cancellationTokenSources, [builtLocalDirectory].concat(cancellationTokenSources), /*prefixes*/ [copyright], /*useBuiltCompiler*/ true, { outDir: builtLocalDirectory, noOutFile: true }); +compileFile(cancellationTokenFile, cancellationTokenSources, [builtLocalDirectory].concat(cancellationTokenSources), /*prefixes*/ [copyright], /*useBuiltCompiler*/ true, { types: ["node"], outDir: builtLocalDirectory, noOutFile: true, lib: "es6" }); var typingsInstallerFile = path.join(builtLocalDirectory, "typingsInstaller.js"); -compileFile(typingsInstallerFile, typingsInstallerSources, [builtLocalDirectory].concat(typingsInstallerSources), /*prefixes*/ [copyright], /*useBuiltCompiler*/ true, { outDir: builtLocalDirectory, noOutFile: false }); +compileFile(typingsInstallerFile, typingsInstallerSources, [builtLocalDirectory].concat(typingsInstallerSources), /*prefixes*/ [copyright], /*useBuiltCompiler*/ true, { types: ["node"], outDir: builtLocalDirectory, noOutFile: false, lib: "es6,scripthost" }); var watchGuardFile = path.join(builtLocalDirectory, "watchGuard.js"); -compileFile(watchGuardFile, watchGuardSources, [builtLocalDirectory].concat(watchGuardSources), /*prefixes*/ [copyright], /*useBuiltCompiler*/ true, { outDir: builtLocalDirectory, noOutFile: false }); +compileFile(watchGuardFile, watchGuardSources, [builtLocalDirectory].concat(watchGuardSources), /*prefixes*/ [copyright], /*useBuiltCompiler*/ true, { types: ["node"], outDir: builtLocalDirectory, noOutFile: false, lib: "es6" }); var serverFile = path.join(builtLocalDirectory, "tsserver.js"); -compileFile(serverFile, serverSources, [builtLocalDirectory, copyright, cancellationTokenFile, typingsInstallerFile, watchGuardFile].concat(serverSources), /*prefixes*/ [copyright], /*useBuiltCompiler*/ true, { types: ["node"], preserveConstEnums: true }); +compileFile(serverFile, serverSources, [builtLocalDirectory, copyright, cancellationTokenFile, typingsInstallerFile, watchGuardFile].concat(serverSources).concat(servicesSources), /*prefixes*/ [copyright], /*useBuiltCompiler*/ true, { types: ["node"], preserveConstEnums: true, lib: "es6,scripthost" }); var tsserverLibraryFile = path.join(builtLocalDirectory, "tsserverlibrary.js"); var tsserverLibraryDefinitionFile = path.join(builtLocalDirectory, "tsserverlibrary.d.ts"); compileFile( @@ -700,7 +706,7 @@ compileFile( /*prereqs*/[builtLocalDirectory, tscFile].concat(libraryTargets).concat(servicesSources).concat(harnessSources), /*prefixes*/[], /*useBuiltCompiler:*/ true, - /*opts*/ { inlineSourceMap: true, types: ["node", "mocha", "chai"] }); + /*opts*/ { inlineSourceMap: true, types: ["node", "mocha", "chai"], lib: "es6,scripthost" }); var internalTests = "internal/"; @@ -1077,7 +1083,7 @@ desc("Compiles tslint rules to js"); task("build-rules", ["build-rules-start"].concat(tslintRulesOutFiles).concat(["build-rules-end"])); tslintRulesFiles.forEach(function (ruleFile, i) { compileFile(tslintRulesOutFiles[i], [ruleFile], [ruleFile], [], /*useBuiltCompiler*/ false, - { noOutFile: true, generateDeclarations: false, outDir: path.join(builtLocalDirectory, "tslint") }); + { noOutFile: true, generateDeclarations: false, outDir: path.join(builtLocalDirectory, "tslint"), lib: "es6" }); }); desc("Emit the start of the build-rules fold"); diff --git a/scripts/parallel-lint.js b/scripts/parallel-lint.js index aec9960ce47..2ac84667d6f 100644 --- a/scripts/parallel-lint.js +++ b/scripts/parallel-lint.js @@ -1,5 +1,6 @@ var tslint = require("tslint"); var fs = require("fs"); +var path = require("path"); function getLinterOptions() { return { @@ -9,7 +10,7 @@ function getLinterOptions() { }; } function getLinterConfiguration() { - return require("../tslint.json"); + return tslint.Configuration.loadConfigurationFromPath(path.join(__dirname, "../tslint.json")); } function lintFileContents(options, configuration, path, contents) { diff --git a/src/harness/tsconfig.json b/src/harness/tsconfig.json index bea688d358b..0dc00f48668 100644 --- a/src/harness/tsconfig.json +++ b/src/harness/tsconfig.json @@ -6,6 +6,10 @@ "declaration": false, "types": [ "node", "mocha", "chai" + ], + "lib": [ + "es6", + "scripthost" ] }, "files": [ diff --git a/src/server/builder.ts b/src/server/builder.ts index a5e7b06e64b..e056f0ae8c7 100644 --- a/src/server/builder.ts +++ b/src/server/builder.ts @@ -1,7 +1,6 @@ /// /// /// -/// namespace ts.server { diff --git a/src/server/cancellationToken/tsconfig.json b/src/server/cancellationToken/tsconfig.json index fa7f88ca994..604b92b4cf2 100644 --- a/src/server/cancellationToken/tsconfig.json +++ b/src/server/cancellationToken/tsconfig.json @@ -5,6 +5,9 @@ "module": "commonjs", "types": [ "node" + ], + "lib": [ + "es6" ] }, "files": [ diff --git a/src/server/server.ts b/src/server/server.ts index 2a5990f4252..a9284556c60 100644 --- a/src/server/server.ts +++ b/src/server/server.ts @@ -131,6 +131,14 @@ namespace ts.server { constructor(private readonly logFilename: string, private readonly traceToConsole: boolean, private readonly level: LogLevel) { + if (this.logFilename) { + try { + this.fd = fs.openSync(this.logFilename, "w"); + } + catch (e) { + // swallow the error and keep logging disabled if file cannot be opened + } + } } static padStringRight(str: string, padding: string) { @@ -175,11 +183,6 @@ namespace ts.server { } msg(s: string, type: Msg.Types = Msg.Err) { - if (this.fd < 0) { - if (this.logFilename) { - this.fd = fs.openSync(this.logFilename, "w"); - } - } if (this.fd >= 0 || this.traceToConsole) { s = s + "\n"; const prefix = Logger.padStringRight(type + " " + this.seq.toString(), " "); @@ -410,6 +413,9 @@ namespace ts.server { } function parseLoggingEnvironmentString(logEnvStr: string): LogOptions { + if (!logEnvStr) { + return {}; + } const logEnv: LogOptions = { logToFile: true }; const args = logEnvStr.split(" "); const len = args.length - 1; @@ -422,8 +428,8 @@ namespace ts.server { logEnv.file = stripQuotes(value); break; case "-level": - const level: LogLevel = (LogLevel)[value]; - logEnv.detailLevel = typeof level === "number" ? level : LogLevel.normal; + const level = getLogLevel(value); + logEnv.detailLevel = level !== undefined ? level : LogLevel.normal; break; case "-traceToConsole": logEnv.traceToConsole = value.toLowerCase() === "true"; @@ -437,28 +443,32 @@ namespace ts.server { return logEnv; } - // TSS_LOG "{ level: "normal | verbose | terse", file?: string}" - function createLoggerFromEnv() { - let fileName: string = undefined; - let detailLevel = LogLevel.normal; - let traceToConsole = false; - const logEnvStr = process.env["TSS_LOG"]; - if (logEnvStr) { - const logEnv = parseLoggingEnvironmentString(logEnvStr); - if (logEnv.logToFile) { - if (logEnv.file) { - fileName = logEnv.file; - } - else { - fileName = __dirname + "/.log" + process.pid.toString(); + function getLogLevel(level: string) { + if (level) { + const l = level.toLowerCase(); + for (const name in LogLevel) { + if (isNaN(+name) && l === name.toLowerCase()) { + return LogLevel[name]; } } - if (logEnv.detailLevel) { - detailLevel = logEnv.detailLevel; - } - traceToConsole = logEnv.traceToConsole; } - return new Logger(fileName, traceToConsole, detailLevel); + return undefined; + } + + // TSS_LOG "{ level: "normal | verbose | terse", file?: string}" + function createLogger() { + const cmdLineLogFileName = findArgument("--logFile"); + const cmdLineVerbosity = getLogLevel(findArgument("--logVerbosity")); + const envLogOptions = parseLoggingEnvironmentString(process.env["TSS_LOG"]); + + const logFileName = cmdLineLogFileName + ? stripQuotes(cmdLineLogFileName) + : envLogOptions.logToFile + ? envLogOptions.file || (__dirname + "/.log" + process.pid.toString()) + : undefined; + + const logVerbosity = cmdLineVerbosity || envLogOptions.detailLevel; + return new Logger(logFileName, envLogOptions.traceToConsole, logVerbosity) } // This places log file in the directory containing editorServices.js // TODO: check that this location is writable @@ -555,7 +565,6 @@ namespace ts.server { // to increase the chunk size or decrease the interval // time dynamically to match the large reference set? const pollingWatchedFileSet = createPollingWatchedFileSet(); - const logger = createLoggerFromEnv(); const pending: Buffer[] = []; let canWrite = true; @@ -607,6 +616,8 @@ namespace ts.server { return s.length > 2 && s.charCodeAt(0) === CharacterCodes.slash && s.charCodeAt(1) === CharacterCodes.slash; } + const logger = createLogger(); + const sys = ts.sys; // use watchGuard process on Windows when node version is 4 or later const useWatchGuard = process.platform === "win32" && getNodeMajorVersion() >= 4; diff --git a/src/server/tsconfig.library.json b/src/server/tsconfig.library.json index 76d700dd291..e47600f9f52 100644 --- a/src/server/tsconfig.library.json +++ b/src/server/tsconfig.library.json @@ -10,7 +10,8 @@ "target": "es5", "noUnusedLocals": true, "noUnusedParameters": true, - "declaration": true + "declaration": true, + "types": [] }, "files": [ "editorServices.ts", diff --git a/src/server/typingsInstaller/nodeTypingsInstaller.ts b/src/server/typingsInstaller/nodeTypingsInstaller.ts index ff20e89e2d7..47f12ea793b 100644 --- a/src/server/typingsInstaller/nodeTypingsInstaller.ts +++ b/src/server/typingsInstaller/nodeTypingsInstaller.ts @@ -13,14 +13,20 @@ namespace ts.server.typingsInstaller { } = require("path"); class FileLog implements Log { + private logEnabled = true; constructor(private readonly logFile?: string) { } isEnabled() { - return this.logFile !== undefined; + return this.logEnabled && this.logFile !== undefined; } writeLine(text: string) { - fs.appendFileSync(this.logFile, text + sys.newLine); + try { + fs.appendFileSync(this.logFile, text + sys.newLine); + } + catch (e) { + this.logEnabled = false; + } } } diff --git a/src/server/typingsInstaller/tsconfig.json b/src/server/typingsInstaller/tsconfig.json index 7bfb6c8b1ed..4cfa26f8d9c 100644 --- a/src/server/typingsInstaller/tsconfig.json +++ b/src/server/typingsInstaller/tsconfig.json @@ -5,6 +5,10 @@ "outFile": "../../../built/local/typingsInstaller.js", "types": [ "node" + ], + "lib": [ + "es6", + "scripthost" ] }, "files": [ diff --git a/src/server/watchGuard/tsconfig.json b/src/server/watchGuard/tsconfig.json index ef9b0ab0603..354d3d7f499 100644 --- a/src/server/watchGuard/tsconfig.json +++ b/src/server/watchGuard/tsconfig.json @@ -1,8 +1,14 @@ { - "extends": "../../tsconfig-base", + "extends": "../../tsconfig-base", "compilerOptions": { "removeComments": true, - "outFile": "../../../built/local/watchGuard.js" + "outFile": "../../../built/local/watchGuard.js", + "types": [ + "node" + ], + "lib": [ + "es6" + ] }, "files": [ "watchGuard.ts"