Skip to content

Commit

Permalink
fix #3307: regression with tsconfig baseURL
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Aug 11, 2023
1 parent a973f87 commit 1fca4aa
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 14 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

* Fix a regression with `baseURL` in `tsconfig.json` ([#3307](https://github.com/evanw/esbuild/issues/3307))

The previous release moved `tsconfig.json` path resolution before `--packages=external` checks to allow the [`paths` field](https://www.typescriptlang.org/tsconfig#paths) in `tsconfig.json` to avoid a package being marked as external. However, that reordering accidentally broke the behavior of the `baseURL` field from `tsconfig.json`. This release moves these path resolution rules around again in an attempt to allow both of these cases to work.

* Parse TypeScript type arguments for JavaScript decorators ([#3308](https://github.com/evanw/esbuild/issues/3308))

When parsing JavaScript decorators in TypeScript (i.e. with `experimentalDecorators` disabled), esbuild previously didn't parse type arguments. Type arguments will now be parsed starting with this release. For example:
Expand Down
27 changes: 27 additions & 0 deletions internal/bundler_tests/bundler_tsconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2511,3 +2511,30 @@ func TestTsconfigJsonTopLevelMistakeWarning(t *testing.T) {
`,
})
}

// https://github.com/evanw/esbuild/issues/3307
func TestTsconfigJsonBaseUrlIssue3307(t *testing.T) {
tsconfig_suite.expectBundled(t, bundled{
files: map[string]string{
"/Users/user/project/src/tsconfig.json": `
{
"compilerOptions": {
"baseUrl": "./subdir"
}
}
`,
"/Users/user/project/src/test.ts": `
export const foo = "well, this is correct...";
`,
"/Users/user/project/src/subdir/test.ts": `
export const foo = "WRONG";
`,
},
entryPaths: []string{"test.ts"},
absWorkingDir: "/Users/user/project/src",
options: config.Options{
Mode: config.ModeBundle,
AbsOutputFile: "/Users/user/project/out.js",
},
})
}
9 changes: 9 additions & 0 deletions internal/bundler_tests/snapshots/snapshots_tsconfig.txt
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,15 @@ var require_util = __commonJS({
var import_util = __toESM(require_util());
console.log((0, import_util.default)());

================================================================================
TestTsconfigJsonBaseUrlIssue3307
---------- /Users/user/project/out.js ----------
// test.ts
var foo = "well, this is correct...";
export {
foo
};

================================================================================
TestTsconfigJsonCommentAllowed
---------- /Users/user/project/out.js ----------
Expand Down
25 changes: 11 additions & 14 deletions internal/resolver/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -1000,20 +1000,9 @@ func (r resolverQuery) resolveWithoutSymlinks(sourceDir string, sourceDirInfo *d

// First, check path overrides from the nearest enclosing TypeScript "tsconfig.json" file
if sourceDirInfo != nil {
if tsConfigJSON := r.tsConfigForDir(sourceDirInfo); tsConfigJSON != nil {
// Try path substitutions first
if tsConfigJSON.Paths != nil {
if absolute, ok, diffCase := r.matchTSConfigPaths(tsConfigJSON, importPath); ok {
return &ResolveResult{PathPair: absolute, DifferentCase: diffCase}
}
}

// Try looking up the path relative to the base URL
if tsConfigJSON.BaseURL != nil {
basePath := r.fs.Join(*tsConfigJSON.BaseURL, importPath)
if absolute, ok, diffCase := r.loadAsFileOrDirectory(basePath); ok {
return &ResolveResult{PathPair: absolute, DifferentCase: diffCase}
}
if tsConfigJSON := r.tsConfigForDir(sourceDirInfo); tsConfigJSON != nil && tsConfigJSON.Paths != nil {
if absolute, ok, diffCase := r.matchTSConfigPaths(tsConfigJSON, importPath); ok {
return &ResolveResult{PathPair: absolute, DifferentCase: diffCase}
}
}
}
Expand Down Expand Up @@ -2318,6 +2307,14 @@ func (r resolverQuery) loadNodeModules(importPath string, dirInfo *dirInfo, forb
defer r.debugLogs.decreaseIndent()
}

// Try looking up the path relative to the base URL from "tsconfig.json"
if tsConfigJSON := r.tsConfigForDir(dirInfo); tsConfigJSON != nil && tsConfigJSON.BaseURL != nil {
basePath := r.fs.Join(*tsConfigJSON.BaseURL, importPath)
if absolute, ok, diffCase := r.loadAsFileOrDirectory(basePath); ok {
return absolute, true, diffCase
}
}

// Find the parent directory with the "package.json" file
dirInfoPackageJSON := dirInfo
for dirInfoPackageJSON != nil && dirInfoPackageJSON.packageJSON == nil {
Expand Down

0 comments on commit 1fca4aa

Please sign in to comment.