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

Paths configuration prefixes regular modules with specified path #8375

Closed
segersniels opened this issue Dec 4, 2023 · 7 comments · Fixed by #8521 or #8535
Closed

Paths configuration prefixes regular modules with specified path #8375

segersniels opened this issue Dec 4, 2023 · 7 comments · Fixed by #8521 or #8535
Assignees
Labels
Milestone

Comments

@segersniels
Copy link

segersniels commented Dec 4, 2023

Describe the bug

For some reason if you specify a baseUrl and paths config like you would otherwise in tsconfig, SWC decides to try to resolve regular node_modules from the specified paths.

Input code

import { helloWorld } from "hello";
import * as Sentry from '@sentry/node';

Sentry;
helloWorld("SWC");

Config

{
  "inlineSourcesContent": true,
  "jsc": {
    "keepClassNames": true,
    "output": {
      "preamble": ""
    },
    "parser": {
      "syntax": "typescript"
    },
    "baseUrl": "/Users/nielssegers/personal/swc-paths/",
    "paths": {
      "*": [
        "src/*"
      ]
    },
    "preserveAllComments": true,
    "target": "es2022",
    "transform": {
      "decoratorMetadata": false,
      "legacyDecorator": false
    }
  },
  "minify": false,
  "module": {
    "allowTopLevelThis": false,
    "ignoreDynamic": false,
    "lazy": false,
    "noInterop": false,
    "preserveImportMeta": false,
    "resolveFully": false,
    "strict": false,
    "strictMode": true,
    "type": "commonjs"
  },
  "sourceMaps": true
}

Playground link (or link to the minimal reproduction)

https://github.com/segersniels/swc-paths-issue

SWC Info output

npx -y swc-info@latest

    Operating System:
        Platform: darwin
        Arch: arm64
        Machine Type: arm64
        Version: Darwin Kernel Version 23.1.0: Mon Oct  9 21:28:12 PDT 2023; root:xnu-10002.41.9~6/RELEASE_ARM64_T8103
        CPU: (8 cores)
            Models: Apple M1

    Binaries:
        Node: 20.9.0
        npm: 10.1.0
        Yarn: 1.22.21
        pnpm: N/A

    Relevant Packages:
        @swc/core: 1.3.100
        @swc/helpers: N/A
        @swc/types: 0.1.5


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

    Next.js info:
        output: N/A

Expected behavior

I would expect swc to correctly differentiate between a local file and an actual module from node_modules that node needs to resolve.

Actual behavior

Instead of leaving a regular module @sentry/node alone, swc decided to add the src/ prefix to the output which stops it from resolving since it thinks it's a local file.

Run npm run build in the example and look at the output of dist/index.js.

const _hello = require("./src/hello");
const _node = _interop_require_wildcard(require("src/@sentry/node"));

Version

0.1.63

Additional context

No response

@kdy1 kdy1 added this to the Planned milestone Dec 4, 2023
@kdy1 kdy1 self-assigned this Dec 4, 2023
@segersniels
Copy link
Author

segersniels commented Dec 4, 2023

For now I'm just working around it by moving the entrypoint of the application to the src directory and changing the paths configuration to:

{
    "baseUrl": "./src",
    "paths": {
      "*": ["*"]
    }
}

That way the src prefix can be removed.

@segersniels
Copy link
Author

segersniels commented Jan 19, 2024

I see that the issue is seemingly fixed. When is the fix expected to release? I have tried with the latest versions of both the crates and npm packages and the issue still seems to be present.

@kdy1
Copy link
Member

kdy1 commented Jan 19, 2024

I think it's not fixed correctly, sorry

@kdy1 kdy1 reopened this Jan 19, 2024
@segersniels
Copy link
Author

segersniels commented Jan 19, 2024

I think it's not fixed correctly, sorry

No worries, here to assist if you need more information! Updated the playground repo to the latest dependencies.

kdy1 added a commit that referenced this issue Jan 21, 2024
@segersniels
Copy link
Author

@kdy1 Is the fix included in the latest published version? Updated the playground example to the latest version but issue is still present.

@kdy1
Copy link
Member

kdy1 commented Jan 22, 2024

Nope, but it should be included in 1.3.106-nightly-20240122.2

@kdy1 kdy1 modified the milestones: Planned, v1.3.106 Jan 24, 2024
@swc-bot
Copy link
Collaborator

swc-bot commented Feb 23, 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 Feb 23, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
3 participants