Skip to content

Commit

Permalink
Add allowPlugins on commandline which is needed to resolve watchFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
sheetalkamat committed Apr 10, 2023
1 parent 05cd11c commit f660f6b
Show file tree
Hide file tree
Showing 56 changed files with 1,455 additions and 170 deletions.
57 changes: 52 additions & 5 deletions src/compiler/commandLineParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,11 @@ import {
getSupportedExtensions,
getSupportedExtensionsWithJsonIfResolveJsonModule,
getTextOfPropertyName,
getTsConfigObjectLiteralExpression,
getTsConfigPropArrayElementValue,
hasExtension,
hasProperty,
identity,
ImportsNotUsedAsValues,
isArray,
isArrayLiteralExpression,
Expand Down Expand Up @@ -527,6 +529,14 @@ export const commonOptionsWithBuild: CommandLineOption[] = [
description: Diagnostics.Set_the_language_of_the_messaging_from_TypeScript_This_does_not_affect_emit,
defaultValueDescription: Diagnostics.Platform_specific
},
{
name: "allowPlugins",
type: "boolean",
category: Diagnostics.Command_line_Options,
isCommandLineOnly: true,
description: Diagnostics.Allow_running_plugins,
defaultValueDescription: false,
},
];

/** @internal */
Expand Down Expand Up @@ -1821,6 +1831,10 @@ export function parseCommandLineWorker(
const errors: Diagnostic[] = [];

parseStrings(commandLine);
if (options.watch && watchOptions?.watchFactory && !options.allowPlugins) {
errors.push(createCompilerDiagnostic(Diagnostics.Option_watchFactory_cannot_be_specified_without_passing_allowPlugins_on_command_line));
watchOptions.watchFactory = undefined;
}
return {
options,
watchOptions,
Expand Down Expand Up @@ -2098,6 +2112,7 @@ export function getParsedCommandLineOfConfigFile(
extendedConfigCache?: Map<string, ExtendedConfigCacheEntry>,
watchOptionsToExtend?: WatchOptions,
extraFileExtensions?: readonly FileExtensionInfo[],
checkAllowPlugins?: boolean,
): ParsedCommandLine | undefined {
const configFileText = tryReadFile(configFileName, fileName => host.readFile(fileName));
if (!isString(configFileText)) {
Expand All @@ -2119,7 +2134,8 @@ export function getParsedCommandLineOfConfigFile(
/*resolutionStack*/ undefined,
extraFileExtensions,
extendedConfigCache,
watchOptionsToExtend
watchOptionsToExtend,
checkAllowPlugins,
);
}

Expand Down Expand Up @@ -2854,9 +2870,20 @@ export function parseJsonConfigFileContent(json: any, host: ParseConfigHost, bas
* @param basePath A root directory to resolve relative path entries in the config
* file to. e.g. outDir
*/
export function parseJsonSourceFileConfigFileContent(sourceFile: TsConfigSourceFile, host: ParseConfigHost, basePath: string, existingOptions?: CompilerOptions, configFileName?: string, resolutionStack?: Path[], extraFileExtensions?: readonly FileExtensionInfo[], extendedConfigCache?: Map<string, ExtendedConfigCacheEntry>, existingWatchOptions?: WatchOptions): ParsedCommandLine {
export function parseJsonSourceFileConfigFileContent(
sourceFile: TsConfigSourceFile,
host: ParseConfigHost,
basePath: string,
existingOptions?: CompilerOptions,
configFileName?: string,
resolutionStack?: Path[],
extraFileExtensions?: readonly FileExtensionInfo[],
extendedConfigCache?: Map<string, ExtendedConfigCacheEntry>,
existingWatchOptions?: WatchOptions,
checkAllowPlugins?: boolean,
): ParsedCommandLine {
tracing?.push(tracing.Phase.Parse, "parseJsonSourceFileConfigFileContent", { path: sourceFile.fileName });
const result = parseJsonConfigFileContentWorker(/*json*/ undefined, sourceFile, host, basePath, existingOptions, existingWatchOptions, configFileName, resolutionStack, extraFileExtensions, extendedConfigCache);
const result = parseJsonConfigFileContentWorker(/*json*/ undefined, sourceFile, host, basePath, existingOptions, existingWatchOptions, configFileName, resolutionStack, extraFileExtensions, extendedConfigCache, checkAllowPlugins);
tracing?.pop();
return result;
}
Expand Down Expand Up @@ -2900,7 +2927,8 @@ function parseJsonConfigFileContentWorker(
configFileName?: string,
resolutionStack: Path[] = [],
extraFileExtensions: readonly FileExtensionInfo[] = [],
extendedConfigCache?: Map<string, ExtendedConfigCacheEntry>
extendedConfigCache?: Map<string, ExtendedConfigCacheEntry>,
checkAllowPlugins?: boolean,
): ParsedCommandLine {
Debug.assert((json === undefined && sourceFile !== undefined) || (json !== undefined && sourceFile === undefined));
const errors: Diagnostic[] = [];
Expand All @@ -2912,6 +2940,21 @@ function parseJsonConfigFileContentWorker(
extend(existingWatchOptions || {}, parsedConfig.watchOptions || {}) :
undefined;

if (options.watch && watchOptions?.watchFactory && checkAllowPlugins && !options.allowPlugins) {
const watchFactoryNameProp = forEachPropertyAssignment(
getTsConfigObjectLiteralExpression(sourceFile),
"watchOptions",
prop => isObjectLiteralExpression(prop.initializer) ?
forEachPropertyAssignment(prop.initializer, "watchFactory", watchFactoryProp => isObjectLiteralExpression(watchFactoryProp.initializer) ?
forEachPropertyAssignment(watchFactoryProp.initializer, "name", identity) || watchFactoryProp :
watchFactoryProp
) :
undefined
);
errors.push(createDiagnosticForNodeInSourceFileOrCompilerDiagnostic(sourceFile, watchFactoryNameProp?.name, Diagnostics.Option_watchFactory_cannot_be_specified_without_passing_allowPlugins_on_command_line));
watchOptions.watchFactory = undefined;
}

options.configFilePath = configFileName && normalizeSlashes(configFileName);
const configFileSpecs = getConfigFileSpecs();
if (sourceFile) sourceFile.configFileSpecs = configFileSpecs;
Expand Down Expand Up @@ -3535,7 +3578,11 @@ export function convertJsonOption(
convertJsonOptionOfListType(opt, value, basePath, errors, propertyAssignment, valueExpression as ArrayLiteralExpression | undefined, sourceFile) :
convertJsonOption(opt.element, value, basePath, errors, propertyAssignment, valueExpression, sourceFile);
}
else if (!isString(opt.type)) {
if (opt.isCommandLineOnly) {
errors.push(createDiagnosticForNodeInSourceFileOrCompilerDiagnostic(sourceFile, valueExpression, Diagnostics.Option_0_can_only_be_specified_on_command_line, opt.name));
return;
}
if (!isString(opt.type)) {
return convertJsonOptionOfCustomType(opt as CommandLineOptionOfCustomType, value as string, errors, valueExpression, sourceFile);
}
const validatedValue = validateJsonOptionValue(opt, value, errors, valueExpression, sourceFile);
Expand Down
8 changes: 8 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -4337,6 +4337,10 @@
"category": "Error",
"code": 5109
},
"Option 'watchFactory' cannot be specified without passing '--allowPlugins' on command line.": {
"category": "Error",
"code": 5110
},

"Generates a sourcemap for each corresponding '.d.ts' file.": {
"category": "Message",
Expand Down Expand Up @@ -6107,6 +6111,10 @@
"category": "Message",
"code": 6804
},
"Allow running plugins.": {
"category": "Message",
"code": 6805
},

"one of:": {
"category": "Message",
Expand Down
3 changes: 2 additions & 1 deletion src/compiler/tsbuildPublic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ export interface BuildOptions {
/** @internal */ locale?: string;
/** @internal */ generateCpuProfile?: string;
/** @internal */ generateTrace?: string;
/** @internal */ allowPlugins?: boolean;

[option: string]: CompilerOptionsValue | undefined;
}
Expand Down Expand Up @@ -567,7 +568,7 @@ function parseConfigFile<T extends BuilderProgram>(state: SolutionBuilderState<T
}
else {
parseConfigFileHost.onUnRecoverableConfigFileDiagnostic = d => diagnostic = d;
parsed = getParsedCommandLineOfConfigFile(configFileName, baseCompilerOptions, parseConfigFileHost, extendedConfigCache, baseWatchOptions);
parsed = getParsedCommandLineOfConfigFile(configFileName, baseCompilerOptions, parseConfigFileHost, extendedConfigCache, baseWatchOptions, /*extraFileExtensions*/ undefined, state.hostWithWatch.checkAllowPlugins);
parseConfigFileHost.onUnRecoverableConfigFileDiagnostic = noop;
}
configFileCache.set(configFilePath, parsed || diagnostic!);
Expand Down
1 change: 1 addition & 0 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7158,6 +7158,7 @@ export interface CompilerOptions {
*/
pathsBasePath?: string;
/** @internal */ plugins?: PluginImport[];
/** @internal */ allowPlugins?: boolean;
preserveConstEnums?: boolean;
noImplicitOverride?: boolean;
preserveSymlinks?: boolean;
Expand Down
12 changes: 10 additions & 2 deletions src/compiler/watch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,10 +213,18 @@ export function createWatchStatusReporter(system: System, pretty?: boolean): Wat
*
* @internal
*/
export function parseConfigFileWithSystem(configFileName: string, optionsToExtend: CompilerOptions, extendedConfigCache: Map<string, ExtendedConfigCacheEntry> | undefined, watchOptionsToExtend: WatchOptions | undefined, system: System, reportDiagnostic: DiagnosticReporter): ParsedCommandLine | undefined {
export function parseConfigFileWithSystem(
configFileName: string,
optionsToExtend: CompilerOptions,
extendedConfigCache: Map<string, ExtendedConfigCacheEntry> | undefined,
watchOptionsToExtend: WatchOptions | undefined,
system: System,
reportDiagnostic: DiagnosticReporter,
checkAllowPlugins: boolean,
): ParsedCommandLine | undefined {
const host: ParseConfigFileHost = system as any;
host.onUnRecoverableConfigFileDiagnostic = diagnostic => reportUnrecoverableDiagnostic(system, reportDiagnostic, diagnostic);
const result = getParsedCommandLineOfConfigFile(configFileName, optionsToExtend, host, extendedConfigCache, watchOptionsToExtend);
const result = getParsedCommandLineOfConfigFile(configFileName, optionsToExtend, host, extendedConfigCache, watchOptionsToExtend, /*extraFileExtensions*/ undefined, checkAllowPlugins);
host.onUnRecoverableConfigFileDiagnostic = undefined!; // TODO: GH#18217
return result;
}
Expand Down
8 changes: 6 additions & 2 deletions src/compiler/watchPublic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ export interface WatchHost {
setTimeout?(callback: (...args: any[]) => void, ms: number, ...args: any[]): any;
/** If provided, will be used to reset existing delayed compilation */
clearTimeout?(timeoutId: any): void;
/** @internal */ checkAllowPlugins?: boolean;
}
export interface ProgramHost<T extends BuilderProgram> {
/**
Expand Down Expand Up @@ -902,7 +903,8 @@ export function createWatchProgram<T extends BuilderProgram>(host: WatchCompiler
parseConfigFileHost,
extendedConfigCache ||= new Map(),
watchOptionsToExtend,
extraFileExtensions
extraFileExtensions,
host.checkAllowPlugins,
)!); // TODO: GH#18217
}

Expand Down Expand Up @@ -962,7 +964,9 @@ export function createWatchProgram<T extends BuilderProgram>(host: WatchCompiler
/*optionsToExtend*/ undefined,
parseConfigFileHost,
extendedConfigCache ||= new Map(),
watchOptionsToExtend
watchOptionsToExtend,
host.extraFileExtensions,
host.checkAllowPlugins,
);
parseConfigFileHost.onUnRecoverableConfigFileDiagnostic = onUnRecoverableConfigFileDiagnostic;
return parsedCommandLine;
Expand Down
4 changes: 3 additions & 1 deletion src/executeCommandLine/executeCommandLine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -645,7 +645,7 @@ function executeCommandLineWorker(
);
if (configFileName) {
const extendedConfigCache = new Map<string, ExtendedConfigCacheEntry>();
const configParseResult = parseConfigFileWithSystem(configFileName, commandLineOptions, extendedConfigCache, commandLine.watchOptions, sys, reportDiagnostic)!; // TODO: GH#18217
const configParseResult = parseConfigFileWithSystem(configFileName, commandLineOptions, extendedConfigCache, commandLine.watchOptions, sys, reportDiagnostic, /*checkAllowPlugins*/ true)!; // TODO: GH#18217
if (commandLineOptions.showConfig) {
if (configParseResult.errors.length !== 0) {
reportDiagnostic = updateReportDiagnostic(
Expand Down Expand Up @@ -847,6 +847,7 @@ function performBuild(
);
const solutionPerformance = enableSolutionPerformance(sys, buildOptions);
updateSolutionBuilderHost(sys, cb, buildHost, solutionPerformance);
buildHost.checkAllowPlugins = true;
const onWatchStatusChange = buildHost.onWatchStatusChange;
let reportBuildStatistics = false;
buildHost.onWatchStatusChange = (d, newLine, options, errorCount) => {
Expand Down Expand Up @@ -979,6 +980,7 @@ function updateWatchCompilationHost(
cb: ExecuteCommandLineCallbacks,
watchCompilerHost: WatchCompilerHost<EmitAndSemanticDiagnosticsBuilderProgram>,
) {
watchCompilerHost.checkAllowPlugins = true;
updateCreateProgram(sys, watchCompilerHost, /*isBuildMode*/ false);
const emitFilesUsingBuilder = watchCompilerHost.afterProgramCreate!; // TODO: GH#18217
watchCompilerHost.afterProgramCreate = builderProgram => {
Expand Down
2 changes: 1 addition & 1 deletion src/testRunner/unittests/reuseProgramStructure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,7 @@ describe("unittests:: Reuse program structure:: isProgramUptoDate", () => {
configFileName,
system
})).getCurrentProgram().getProgram();
const { fileNames, options } = ts.parseConfigFileWithSystem(configFileName, {}, /*extendedConfigCache*/ undefined, /*watchOptionsToExtend*/ undefined, system, ts.notImplemented)!; // TODO: GH#18217
const { fileNames, options } = ts.parseConfigFileWithSystem(configFileName, {}, /*extendedConfigCache*/ undefined, /*watchOptionsToExtend*/ undefined, system, ts.notImplemented, /*checkAllowPlugins*/ true)!; // TODO: GH#18217
verifyProgramIsUptoDate(program, fileNames, options);
}

Expand Down
2 changes: 1 addition & 1 deletion src/testRunner/unittests/tsbuild/outputPaths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ describe("unittests:: tsbuild - output file paths", () => {

assert.deepEqual(
ts.getOutputFileNames(
ts.parseConfigFileWithSystem("/src/tsconfig.json", {}, /*extendedConfigCache*/ undefined, {}, sys, ts.noop)!,
ts.parseConfigFileWithSystem("/src/tsconfig.json", {}, /*extendedConfigCache*/ undefined, {}, sys, ts.noop, /*checkAllowPlugins*/ true)!,
"/src/src/index.ts",
/*ignoreCase*/ false
),
Expand Down
Loading

0 comments on commit f660f6b

Please sign in to comment.