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

baseUrl + paths in transform resolves path to parent dir #8660

Closed
devjiwonchoi opened this issue Feb 24, 2024 · 2 comments · Fixed by #8662
Closed

baseUrl + paths in transform resolves path to parent dir #8660

devjiwonchoi opened this issue Feb 24, 2024 · 2 comments · Fixed by #8662
Assignees
Labels
Milestone

Comments

@devjiwonchoi
Copy link
Contributor

devjiwonchoi commented Feb 24, 2024

Describe the bug

SWC transform requires the baseUrl to be absolute path.

The common case is to use resolve to get the absolute path resolving the cwd and baseUrl.

For the following examples, I'm using process.cwd() as resolved baseUrl.

If pass process.cwd() as baseUrl to swc transform, it resolves the path as the parent folder.

It should resolve the path for the baseUrl folder and should not resolve the path for the parent folder.

Input code

import { transform } from '@swc/core'

const input = `
import { foo } from '@/foo'

console.log(foo)
`

async function main() {
  const { code } = await transform(input, {
    jsc: {
      baseUrl: process.cwd(),
      paths: {
        '@/*': ['./src/*'],
      },
    },
  })

  console.log(code)
}

main()

Config

No response

Playground link (or link to the minimal reproduction)

https://github.com/devjiwonchoi/repro-swc-base-url-paths

SWC Info output

Operating System:
    Platform: darwin
    Arch: arm64
    Machine Type: arm64
    Version: Darwin Kernel Version 23.1.0: Mon Oct  9 21:32:11 PDT 2023; root:xnu-10002.41.9~7/RELEASE_ARM64_T6030
    CPU: (11 cores)
        Models: Apple M3 Pro

Binaries:
    Node: 18.17.0
    npm: 9.6.7
    Yarn: 1.22.19
    pnpm: 8.14.2

Relevant Packages:
    @swc/core: 1.4.2
    @swc/helpers: N/A
    @swc/types: N/A
    typescript: 5.3.3

SWC Config:
    output: N/A
    .swcrc path: N/A

Next.js info:
    output: N/A

Expected behavior

It should resolve the path for the baseUrl folder and should not resolve the path for the parent folder.

import { foo } from "./src/foo"; // based on current dir

console.log(foo, bar);

Actual behavior

If pass process.cwd() as baseUrl to swc transform, it resolves the path as the parent folder.
Also, it does not resolve the path for the baseUrl folder.

import { foo } from "./repro-swc-base-url-paths/src/foo"; // parent folder

console.log(foo, bar);

Version

1.4.2

Additional context

@devjiwonchoi devjiwonchoi changed the title baseUrl is not resolved properly baseUrl + paths resolves path to parent dir Feb 24, 2024
@devjiwonchoi devjiwonchoi changed the title baseUrl + paths resolves path to parent dir baseUrl + paths in transform resolves path to parent dir Feb 24, 2024
@kdy1 kdy1 added this to the Planned milestone Feb 26, 2024
@kdy1 kdy1 self-assigned this Feb 26, 2024
kdy1 added a commit that referenced this issue Feb 26, 2024
@devjiwonchoi
Copy link
Contributor Author

Confirmed that it is fixed by v1.4.3-nightly-20240226.1

@swc-bot
Copy link
Collaborator

swc-bot commented Apr 4, 2024

This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@swc-project swc-project locked as resolved and limited conversation to collaborators Apr 4, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Development

Successfully merging a pull request may close this issue.

3 participants