Skip to content

Only consider baseUrl for non-relative imports#1111

Merged
Perryvw merged 1 commit intoTypeScriptToLua:masterfrom
wildbook:fix-relative-imports
Aug 31, 2021
Merged

Only consider baseUrl for non-relative imports#1111
Perryvw merged 1 commit intoTypeScriptToLua:masterfrom
wildbook:fix-relative-imports

Conversation

@wildbook
Copy link
Copy Markdown
Contributor

According to TypeScript's documentation, baseUrl should only be used to resolve absolute imports.

The way imports are currently resolved requires absolute paths for all imports if baseUrl is set, which broke our codebase when we updated TSTL.

Example

Assume that baseUrl is set to source/ and that this is how our code is structured:

 | ...
 | tsconfig.json
 \ source
   | foo.ts
   | bar.ts
   \ baz
     | index.ts
     | bar.ts

If baz/index.ts contains import { Bar } from './bar', that import will resolve to source/bar.ts instead of the correct file source/baz/bar.ts.

If source/bar.ts does not exist, the resolver fallback will successfully assume the correct path, but it will also emit an error that the path could not be resolved. In general we'd much prefer to not ignore errors and resolveDependency on fallbacks, not to mention the errors emitted would make CI builds fail.

Fix

I've fixed this by checking whether or not the import is considered relative before considering options.baseUrl in src/transpilation/resolve.ts:resolveDependency but I don't know if that is the correct way to fix it or not, only that it works in our case and that TSTL's tests pass.

Affects

This change only affects relative imports, and only when baseUrl is set.
The definition used for "relative" is "/, ./, or ../" and comes from the same docs page as linked above.

A relative import is one that starts with /, ./ or ../.

Copy link
Copy Markdown
Member

@Perryvw Perryvw left a comment

Choose a reason for hiding this comment

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

Thanks, looks good!

@Perryvw Perryvw merged commit dd88722 into TypeScriptToLua:master Aug 31, 2021
@wildbook wildbook deleted the fix-relative-imports branch September 1, 2021 17:35
sanikoyes pushed a commit to sanikoyes/TypeScriptToLua that referenced this pull request Sep 24, 2021
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.

2 participants