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

fix(cli): Resolve jsc.baseUrl before calling @swc/cli #2245

Merged
merged 2 commits into from
Aug 18, 2023

Conversation

kdy1
Copy link
Contributor

@kdy1 kdy1 commented Aug 17, 2023

PR Checklist

Please check if your PR fulfills the following requirements:

This is an implicit bugfix.

PR Type

What kind of change does this PR introduce?

[x] Bugfix
[ ] Feature
[ ] Code style update (formatting, local variables)
[ ] Refactoring (no functional changes, no api changes)
[ ] Build related changes
[ ] CI related changes
[ ] Other... Please describe:

What is the current behavior?

@nest/cli does not resolve jsc.baseUrl even though it reads jsc.baseUrl from .swcrc.

See swc-project/swc#7827 for the reason that @swc/core cannot resolve jsc.baseUrl if it's not coming from .swcrc

Issue Number: N/A

What is the new behavior?

It's resolved from the directory of .swcrc, if exists, and the project root (I guess?) if not.

Does this PR introduce a breaking change?

[ ] Yes
[x] No

Other information

I'm author of the SWC project

@kamilmysliwiec
Copy link
Member

Outstanding job on creating SWC, @kdy1!

@kamilmysliwiec kamilmysliwiec merged commit 1bd8775 into nestjs:master Aug 18, 2023
1 check passed
@andrucz
Copy link

andrucz commented Aug 22, 2023

@kamilmysliwiec any plans on releasing this? Been waiting on it before enabling SWC in all our projects 😃

@kamilmysliwiec
Copy link
Member

Just published as 10.1.13

@zhangyuang
Copy link
Contributor

With version '10.1.14', the problem remains in default example

 npx nest start -b swc
>  SWC  Running...
thread 'thread 'thread 'thread '<unnamed><unnamed><unnamed>' panicked at '' panicked at '<unnamed>' panicked at 'base_dir(`Users/yuuang/Desktop/project-name`) must be absolute. Please ensure that `jsc.baseUrl` is specified correctly. This cannot be deduced by SWC itself because SWC is a transpiler and it does not try to resolve project details. In other works, SWC does not know which directory should be used as a base directory. It can be deduced if `.swcrc` is used, but if not, there are many candidates. e.g. the directory containing `package.json`, or the current working directory. Because of that, the caller (typically the developer of the JavaScript package) should specify it. If you see this error, please report an issue to the package author.base_dir(`Users/yuuang/Desktop/project-name`) must be absolute. Please ensure that `jsc.baseUrl` is specified correctly. This cannot be deduced by SWC itself because SWC is a transpiler and it does not try to resolve project details. In other works, SWC does not know which directory should be used as a base directory. It can be deduced if `.swcrc` is used, but if not, there are many candidates. e.g. the directory containing `package.json`, or the current working directory. Because of that, the caller (typically the developer of the JavaScript package) should specify it. If you see this error, please report an issue to the package author.' panicked at 'base_dir(`Users/yuuang/Desktop/project-name`) must be absolute. Please ensure that `jsc.baseUrl` is specified correctly. This cannot be deduced by SWC itself because SWC is a transpiler and it does not try to resolve project details. In other works, SWC does not know which directory should be used as a base directory. It can be deduced if `.swcrc` is used, but if not, there are many candidates. e.g. the directory containing `package.json`, or the current working directory. Because of that, the caller (typically the developer of the JavaScript package) should specify it. If you see this error, please report an issue to the package author.', ', base_dir(`Users/yuuang/Desktop/project-name`) must be absolute. Please ensure that `jsc.baseUrl` is specified correctly. This cannot be deduced by SWC itself because SWC is a transpiler and it does not try to resolve project details. In other works, SWC does not know which directory should be used as a base directory. It can be deduced if `.swcrc` is used, but if not, there are many candidates. e.g. the directory containing `package.json`, or the current working directory. Because of that, the caller (typically the developer of the JavaScript package) should specify it. If you see this error, please report an issue to the package author.', /Users/runner/.cargo/registry/src/index.crates.io-6f17d22bba15001f/swc_ecma_transforms_module-0.173.38/src/path.rs', /Users/runner/.cargo/registry/src/index.crates.io-6f17d22bba15001f/swc_ecma_transforms_module-0.173.38/src/path.rs:/Users/runner/.cargo/registry/src/index.crates.io-6f17d22bba15001f/swc_ecma_transforms_module-0.173.38/src/path.rs:104:10413:/Users/runner/.cargo/registry/src/index.crates.io-6f17d22bba15001f/swc_ecma_transforms_module-0.173.38/src/path.rs
13:note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

:104:13
104:13
thread '<unnamed>' panicked at 'base_dir(`Users/yuuang/Desktop/project-name`) must be absolute. Please ensure that `jsc.baseUrl` is specified correctly. This cannot be deduced by SWC itself because SWC is a transpiler and it does not try to resolve project details. In other works, SWC does not know which directory should be used as a base directory. It can be deduced if `.swcrc` is used, but if not, there are many candidates. e.g. the directory containing `package.json`, or the current working directory. Because of that, the caller (typically the developer of the JavaScript package) should specify it. If you see this error, please report an issue to the package author.', /Users/runner/.cargo/registry/src/index.crates.io-6f17d22bba15001f/swc_ecma_transforms_module-0.173.38/src/path.rs:104:13
failed to handle: base_dir(`Users/yuuang/Desktop/project-name`) must be absolute. Please ensure that `jsc.baseUrl` is specified correctly. This cannot be deduced by SWC itself because SWC is a transpiler and it does not try to resolve project details. In other works, SWC does not know which directory should be used as a base directory. It can be deduced if `.swcrc` is used, but if not, there are many candidates. e.g. the directory containing `package.json`, or the current working directory. Because of that, the caller (typically the developer of the JavaScript package) should specify it. If you see this error, please report an issue to the package author.
failed to handle: base_dir(`Users/yuuang/Desktop/project-name`) must be absolute. Please ensure that `jsc.baseUrl` is specified correctly. This cannot be deduced by SWC itself because SWC is a transpiler and it does not try to resolve project details. In other works, SWC does not know which directory should be used as a base directory. It can be deduced if `.swcrc` is used, but if not, there are many candidates. e.g. the directory containing `package.json`, or the current working directory. Because of that, the caller (typically the developer of the JavaScript package) should specify it. If you see this error, please report an issue to the package author.
failed to handle: base_dir(`Users/yuuang/Desktop/project-name`) must be absolute. Please ensure that `jsc.baseUrl` is specified correctly. This cannot be deduced by SWC itself because SWC is a transpiler and it does not try to resolve project details. In other works, SWC does not know which directory should be used as a base directory. It can be deduced if `.swcrc` is used, but if not, there are many candidates. e.g. the directory containing `package.json`, or the current working directory. Because of that, the caller (typically the developer of the JavaScript package) should specify it. If you see this error, please report an issue to the package author.
failed to handle: base_dir(`Users/yuuang/Desktop/project-name`) must be absolute. Please ensure that `jsc.baseUrl` is specified correctly. This cannot be deduced by SWC itself because SWC is a transpiler and it does not try to resolve project details. In other works, SWC does not know which directory should be used as a base directory. It can be deduced if `.swcrc` is used, but if not, there are many candidates. e.g. the directory containing `package.json`, or the current working directory. Because of that, the caller (typically the developer of the JavaScript package) should specify it. If you see this error, please report an issue to the package author.
failed to handle: base_dir(`Users/yuuang/Desktop/project-name`) must be absolute. Please ensure that `jsc.baseUrl` is specified correctly. This cannot be deduced by SWC itself because SWC is a transpiler and it does not try to resolve project details. In other works, SWC does not know which directory should be used as a base directory. It can be deduced if `.swcrc` is used, but if not, there are many candidates. e.g. the directory containing `package.json`, or the current working directory. Because of that, the caller (typically the developer of the JavaScript package) should specify it. If you see this error, please report an issue to the package author.
Failed to compile 5 files with swc.

 Error  Failed to compile:
src/app.controller.spec.ts
src/app.controller.ts
src/app.module.ts
src/app.service.ts
src/main.ts

@kdy1
Copy link
Contributor Author

kdy1 commented Aug 24, 2023

Users/yuuang/Desktop/project-name is clearly wrong, but I don't have any idea

@zhangyuang
Copy link
Contributor

maybe we can lock swc/core version?

@kdy1
Copy link
Contributor Author

kdy1 commented Aug 24, 2023

Note: base_dir(Users/yuuang/Desktop/project-name) indicates that this is not a bug of SWC. I think the cause is related to the first argument passed to pass.join, which is provided by nestjs somehow.

You can lock in your app.

@zhangyuang
Copy link
Contributor

In my scene lock version is difficult, if the problem belong to nestjs, we should fix it in nest

@kamilmysliwiec
Copy link
Member

@kdy1 should baseUrl be an absolute or relative path? Because Users/yuuang/Desktop/project-name is absolute and SWC says it's incorrect

@kamilmysliwiec
Copy link
Member

kamilmysliwiec commented Aug 24, 2023

When I revert this PR, specifically this change:

image

Everything works as expected (no errors)

image

Before (if we revert this PR):
baseUrl === '/Users/___/Projects/nest/sample/01-cats-app'

After:

baseUrl === 'Users/___/Projects/nest/sample/01-cats-app'

(see no slash at [0])

@kdy1
Copy link
Contributor Author

kdy1 commented Aug 24, 2023

It should be absolute. It does not start with /, and I think that's the cause.

@kdy1
Copy link
Contributor Author

kdy1 commented Aug 24, 2023

I'm not sure why the first slash is removed.

@kamilmysliwiec
Copy link
Member

Let me investigate. I'll create a follow-up PR shortly!

@kdy1
Copy link
Contributor Author

kdy1 commented Aug 24, 2023

Yeah, Path::is_absolute checks for the root prefix. (/ in mac os)

@kamilmysliwiec
Copy link
Member

Fixed deddb98, published as 10.1.15

@kdy1
Copy link
Contributor Author

kdy1 commented Aug 24, 2023

Cool! Thank you!

@zhangyuang
Copy link
Contributor

With nestjs/cli@10.1.16, the default example remain throw error

$ nest new project-name
$ cd project-name && yarn && yarn add @swc/cli @swc/core
$ npx nest start -b swc
>  SWC  Running...
Successfully compiled: 5 files with swc (14.93ms)
Error: Cannot find module 'src/app.module'
Require stack:
- /Users/yuuang/Desktop/project-name/dist/main.js
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:1026:15)
    at Function.Module._load (node:internal/modules/cjs/loader:871:27)
    at Module.require (node:internal/modules/cjs/loader:1098:19)
    at require (node:internal/modules/cjs/helpers:108:18)
    at Object.<anonymous> (/Users/yuuang/Desktop/project-name/dist/main.js:6:20)
    at Module._compile (node:internal/modules/cjs/loader:1196:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1250:10)
    at Module.load (node:internal/modules/cjs/loader:1074:32)
    at Function.Module._load (node:internal/modules/cjs/loader:909:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)

@zhangyuang
Copy link
Contributor

image

@kamilmysliwiec
Copy link
Member

#2271

@nestjs nestjs locked and limited conversation to collaborators Aug 31, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants