Skip to content
This repository was archived by the owner on Nov 16, 2023. It is now read-only.

Commit af9d244

Browse files
committed
Restore cwd after linting finishes
microsoft/TypeScript#35001
1 parent 4fd2032 commit af9d244

File tree

1 file changed

+70
-62
lines changed

1 file changed

+70
-62
lines changed

src/runner/index.ts

Lines changed: 70 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -232,81 +232,89 @@ export class TsLintRunner {
232232
return emptyResult;
233233
}
234234

235+
let cwdToRestore: string | undefined;
235236
if (cwd) {
236237
this.traceMethod('doRun', `Changed directory to ${cwd}`);
238+
cwdToRestore = process.cwd();
237239
process.chdir(cwd);
238240
}
239-
240-
const configFile = configuration.configFile || null;
241-
let linterConfiguration: Configuration | undefined;
242-
this.traceMethod('doRun', 'About to getConfiguration');
241+
243242
try {
244-
linterConfiguration = this.getConfiguration(filePath, filePath, library, configFile);
245-
} catch (err) {
246-
this.traceMethod('doRun', `No linting: exception when getting tslint configuration for ${filePath}, configFile= ${configFile}`);
247-
warnings.push(getConfigurationFailureMessage(err));
248-
return {
249-
lintResult: emptyLintResult,
250-
warnings,
251-
};
252-
}
243+
const configFile = configuration.configFile || null;
244+
let linterConfiguration: Configuration | undefined;
245+
this.traceMethod('doRun', 'About to getConfiguration');
246+
try {
247+
linterConfiguration = this.getConfiguration(filePath, filePath, library, configFile);
248+
} catch (err) {
249+
this.traceMethod('doRun', `No linting: exception when getting tslint configuration for ${filePath}, configFile= ${configFile}`);
250+
warnings.push(getConfigurationFailureMessage(err));
251+
return {
252+
lintResult: emptyLintResult,
253+
warnings,
254+
};
255+
}
253256

254-
if (!linterConfiguration) {
255-
this.traceMethod('doRun', `No linting: no tslint configuration`);
256-
return emptyResult;
257-
}
258-
this.traceMethod('doRun', 'Configuration fetched');
257+
if (!linterConfiguration) {
258+
this.traceMethod('doRun', `No linting: no tslint configuration`);
259+
return emptyResult;
260+
}
261+
this.traceMethod('doRun', 'Configuration fetched');
259262

260-
if (isJsDocument(filePath) && !configuration.jsEnable) {
261-
this.traceMethod('doRun', `No linting: a JS document, but js linting is disabled`);
262-
return emptyResult;
263-
}
263+
if (isJsDocument(filePath) && !configuration.jsEnable) {
264+
this.traceMethod('doRun', `No linting: a JS document, but js linting is disabled`);
265+
return emptyResult;
266+
}
264267

265-
if (configuration.validateWithDefaultConfig === false && this.configCache.configuration!.isDefaultLinterConfig) {
266-
this.traceMethod('doRun', `No linting: linting with default tslint configuration is disabled`);
267-
return emptyResult;
268-
}
268+
if (configuration.validateWithDefaultConfig === false && this.configCache.configuration!.isDefaultLinterConfig) {
269+
this.traceMethod('doRun', `No linting: linting with default tslint configuration is disabled`);
270+
return emptyResult;
271+
}
269272

270-
if (isExcludedFromLinterOptions(linterConfiguration.linterConfiguration, filePath)) {
271-
this.traceMethod('doRun', `No linting: file is excluded using linterOptions.exclude`);
272-
return emptyResult;
273-
}
273+
if (isExcludedFromLinterOptions(linterConfiguration.linterConfiguration, filePath)) {
274+
this.traceMethod('doRun', `No linting: file is excluded using linterOptions.exclude`);
275+
return emptyResult;
276+
}
274277

275-
let result: tslint.LintResult;
276-
const options: tslint.ILinterOptions = {
277-
formatter: "json",
278-
fix: false,
279-
rulesDirectory: configuration.rulesDirectory || undefined,
280-
formattersDirectory: undefined,
281-
};
282-
if (configuration.traceLevel && configuration.traceLevel === 'verbose') {
283-
this.traceConfigurationFile(linterConfiguration.linterConfiguration);
284-
}
278+
let result: tslint.LintResult;
279+
const options: tslint.ILinterOptions = {
280+
formatter: "json",
281+
fix: false,
282+
rulesDirectory: configuration.rulesDirectory || undefined,
283+
formattersDirectory: undefined,
284+
};
285+
if (configuration.traceLevel && configuration.traceLevel === 'verbose') {
286+
this.traceConfigurationFile(linterConfiguration.linterConfiguration);
287+
}
285288

286-
// tslint writes warnings using console.warn, capture these warnings and send them to the client
287-
const originalConsoleWarn = console.warn;
288-
const captureWarnings = (message?: any) => {
289-
warnings.push(message);
290-
originalConsoleWarn(message);
291-
};
292-
console.warn = captureWarnings;
293-
294-
try { // clean up if tslint crashes
295-
const linter = new library.Linter(options, typeof contents === 'string' ? undefined : contents);
296-
this.traceMethod('doRun', `Linting: start linting`);
297-
linter.lint(filePath, typeof contents === 'string' ? contents : '', linterConfiguration.linterConfiguration);
298-
result = linter.getResult();
299-
this.traceMethod('doRun', `Linting: ended linting`);
289+
// tslint writes warnings using console.warn, capture these warnings and send them to the client
290+
const originalConsoleWarn = console.warn;
291+
const captureWarnings = (message?: any) => {
292+
warnings.push(message);
293+
originalConsoleWarn(message);
294+
};
295+
console.warn = captureWarnings;
296+
297+
try { // clean up if tslint crashes
298+
const linter = new library.Linter(options, typeof contents === 'string' ? undefined : contents);
299+
this.traceMethod('doRun', `Linting: start linting`);
300+
linter.lint(filePath, typeof contents === 'string' ? contents : '', linterConfiguration.linterConfiguration);
301+
result = linter.getResult();
302+
this.traceMethod('doRun', `Linting: ended linting`);
303+
} finally {
304+
console.warn = originalConsoleWarn;
305+
}
306+
307+
return {
308+
lintResult: result,
309+
warnings,
310+
workspaceFolderPath: configuration.workspaceFolderPath,
311+
configFilePath: linterConfiguration.path,
312+
};
300313
} finally {
301-
console.warn = originalConsoleWarn;
314+
if (typeof cwdToRestore === 'string') {
315+
process.chdir(cwdToRestore);
316+
}
302317
}
303-
304-
return {
305-
lintResult: result,
306-
warnings,
307-
workspaceFolderPath: configuration.workspaceFolderPath,
308-
configFilePath: linterConfiguration.path,
309-
};
310318
}
311319

312320
private getConfiguration(uri: string, filePath: string, library: typeof tslint, configFileName: string | null): Configuration | undefined {

0 commit comments

Comments
 (0)