Skip to content

check that files specified in tsconfig.json "include" is located in "rootdir"#3783

Open
iisaduan wants to merge 1 commit intomicrosoft:mainfrom
iisaduan:tsconfigRootdirInclude
Open

check that files specified in tsconfig.json "include" is located in "rootdir"#3783
iisaduan wants to merge 1 commit intomicrosoft:mainfrom
iisaduan:tsconfigRootdirInclude

Conversation

@iisaduan
Copy link
Copy Markdown
Member

@iisaduan iisaduan commented May 8, 2026

fixes #3662

Copilot AI review requested due to automatic review settings May 8, 2026 22:06
if checkSourceFilesBelongToPath != nil {
checkSourceFilesBelongToPath(files, options.RootDir)
}
} else if options.ConfigFilePath != "" {
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In strada, this is else if (options.composite && options.configFilePath), but making that change here broke quite a few things. I'm not sure the reasoning for the change, so I left it

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What did it break? I think I remember sheetal doing that on purpose

@iisaduan iisaduan requested review from jakebailey and johnfav03 May 8, 2026 22:09
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes a behavior gap vs upstream TypeScript by reporting TS6059 when a file is included in the program but lies outside the effective rootDir/common source directory, ensuring tsgo aligns with tsc for include/default-include scenarios.

Changes:

  • Add a source-file containment check during common source directory computation to emit TS6059 diagnostics for out-of-root files.
  • Plumb the containment check through outputpaths.GetCommonSourceDirectory and invoke it from both parsed config handling and Program.
  • Add/refresh compiler + build baselines to reflect the newly reported diagnostics and build-info “errors” state.

Reviewed changes

Copilot reviewed 12 out of 12 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
testdata/tests/cases/compiler/tsconfigRootdirInclude.ts New regression test intended to cover include selecting a file outside rootDir.
testdata/baselines/reference/compiler/tsconfigRootdirInclude.errors.txt Expected diagnostics output for the new regression test.
testdata/baselines/reference/compiler/tsconfigRootdirInclude.js Expected JS emit baseline for the new regression test.
testdata/baselines/reference/compiler/tsconfigRootdirInclude.symbols Expected symbols baseline for the new regression test.
testdata/baselines/reference/compiler/tsconfigRootdirInclude.types Expected types baseline for the new regression test.
testdata/baselines/reference/tsc/projectReferences/errors-when-a-file-is-outside-the-rootdir.js Updates reference output to include TS6059 in addition to existing errors.
testdata/baselines/reference/tsbuild/outputPaths/when-rootDir-is-specified-but-not-all-files-belong-to-rootDir.js Updates build baselines to reflect TS6059 and buildinfo “errors” propagation.
testdata/baselines/reference/tsbuild/outputPaths/when-rootDir-is-specified-but-not-all-files-belong-to-rootDir-and-is-composite.js Same as above, for composite builds.
internal/tsoptions/parsedcommandline.go Computes file list and adds a root/common-dir containment check during common source dir computation.
internal/outputpaths/commonsourcedirectory.go Extends GetCommonSourceDirectory to accept files and an optional containment-check callback.
internal/compiler/program.go Adds program-level containment check that reports via include-processing diagnostics for richer “why included” chains.
internal/compiler/emitter.go Updates call site for the new GetCommonSourceDirectory signature.

Comment on lines +3 to +10
"compilerOptions": {
"target": "es2020",
"strictNullChecks": true,
"stableTypeOrdering": true,
"rootDir": "./src2",
"include": ["src1/*"]
}
}
Comment on lines +139 to +146
func (p *ParsedCommandLine) checkSourceFilesBelongToPath(sourceFiles []string, rootDirectory string) bool {
allFilesBelongToPath := true
absoluteRootDirectoryPath := tspath.GetCanonicalFileName(tspath.GetNormalizedAbsolutePath(rootDirectory, p.GetCurrentDirectory()), p.UseCaseSensitiveFileNames())
for _, file := range sourceFiles {
absoluteSourceFilePath := tspath.GetCanonicalFileName(tspath.GetNormalizedAbsolutePath(file, p.GetCurrentDirectory()), p.UseCaseSensitiveFileNames())
if !strings.HasPrefix(absoluteSourceFilePath, absoluteRootDirectoryPath) {
p.Errors = append(p.Errors, ast.NewCompilerDiagnostic(diagnostics.File_0_is_not_under_rootDir_1_rootDir_is_expected_to_contain_all_source_files, file, rootDirectory))
allFilesBelongToPath = false
Comment on lines +1551 to +1557
func (p *Program) checkSourceFilesBelongToPath(sourceFiles []string, rootDirectory string) bool {
allFilesBelongToPath := true
absoluteRootDirectoryPath := tspath.GetCanonicalFileName(tspath.GetNormalizedAbsolutePath(rootDirectory, p.GetCurrentDirectory()), p.UseCaseSensitiveFileNames())
for _, file := range sourceFiles {
absoluteSourceFilePath := tspath.GetCanonicalFileName(tspath.GetNormalizedAbsolutePath(file, p.GetCurrentDirectory()), p.UseCaseSensitiveFileNames())
if !strings.HasPrefix(absoluteSourceFilePath, absoluteRootDirectoryPath) {
p.includeProcessor.addProcessingDiagnostic(&processingDiagnostic{
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Behavior difference: tsgo wrongly allows include to contain files that is not under rootDir from tsconfig

3 participants