Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Absolute paths are not resolved in tsconfig.json paths #792

Closed
iamakulov opened this issue Feb 11, 2021 · 2 comments
Closed

Absolute paths are not resolved in tsconfig.json paths #792

iamakulov opened this issue Feb 11, 2021 · 2 comments

Comments

@iamakulov
Copy link

iamakulov commented Feb 11, 2021

Hey Evan, I believe I found a bug. When tsconfig.json has absolute paths in its compilerOptions.paths, ESBuild doesn’t resolve these paths properly.

Details

Consider the following tsconfig.json (with an absolute path in its alias):

{
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "alias/*": ["/tmp/esbuild-tsconfig/subdir/*"]
    }
  }
}

With this tsconfig.json and files like

// /tmp/esbuild-tsconfig/index.js
import subdep from "alias/subfile";
console.log(subdep);

// /tmp/esbuild-tsconfig/subdir/subfile.ts
export default 5;

ESBuild won’t find alias/subfile:

$ esbuild --bundle index.ts --outdir=dist
 > index.ts: error: Could not resolve "alias/subfile" (mark it as external to exclude it from the bundle)
    1 │ import subdep from "alias/subfile";
      ╵                    ~~~~~~~~~~~~~~~

1 error

The error would go away if we replace the absolute path with the relative one:

{
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "alias/*": ["./subdir/*"]
    }
  }
}

From my experiments, this issue affects at least:

  • the extends field
  • the compilerOptions.paths field

Repro

Repository: https://github.com/iamakulov/esbuild-tsconfig-repro

Steps to reproduce:

# Clone the repository into `/tmp/esbuild-tsconfig`
git clone https://github.com/iamakulov/esbuild-tsconfig-repro.git /tmp/esbuild-tsconfig

# Install dependencies
yarn

# Run ESBuild
yarn esbuild --bundle index.ts --outdir=dist

When this matters

This doesn’t matter in general, since pretty much every tsconfig.json uses relative paths. However, this matters for autogenerated tsconfigs – where relative paths are more complicated to compute (as they are relative to baseUrl which may be inherited). In that case, absolute paths are more convenient. (Semi-related to #783.)

@susiwen8
Copy link
Contributor

susiwen8 commented Feb 12, 2021

I think your path is wrong, according to path-mapping,

Please notice that "paths" are resolved relative to "baseUrl". When setting "baseUrl" to another value than ".", i.e. the directory of tsconfig.json, the mappings must be changed accordingly.

So you should do

{
  "compilerOptions": {
    "baseUrl": ".",
    "outDir": "./dist",
    "paths": {
      "alias/*": ["subdir/*"]
    }
  }
}

@evanw
Copy link
Owner

evanw commented Feb 12, 2021

Thanks for the heads up. This is currently missing test coverage, and is a bug in esbuild. Will fix.

Please notice that "paths" are resolved relative to "baseUrl".

TypeScript's notion of "resolving relative to" involves not doing resolution for absolute directories. Here is the documentation from the actual code in the compiler that ends up doing this:

Combines paths. If a path is absolute, it replaces any previous path. Relative paths are not simplified.

// Non-rooted
combinePaths("path", "to", "file.ext") === "path/to/file.ext"
combinePaths("path", "dir", "..", "to", "file.ext") === "path/dir/../to/file.ext"
// POSIX
combinePaths("/path", "to", "file.ext") === "/path/to/file.ext"
combinePaths("/path", "/to", "file.ext") === "/to/file.ext"
// DOS
combinePaths("c:/path", "to", "file.ext") === "c:/path/to/file.ext"
combinePaths("c:/path", "c:/to", "file.ext") === "c:/to/file.ext"
// URL
combinePaths("file:///path", "to", "file.ext") === "file:///path/to/file.ext"
combinePaths("file:///path", "file:///to", "file.ext") === "file:///to/file.ext"

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 a pull request may close this issue.

3 participants