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

Cannot transpile the working directory itself #8139

Closed
akwodkiewicz opened this issue Oct 18, 2023 · 1 comment
Closed

Cannot transpile the working directory itself #8139

akwodkiewicz opened this issue Oct 18, 2023 · 1 comment
Assignees
Labels
Milestone

Comments

@akwodkiewicz
Copy link

akwodkiewicz commented Oct 18, 2023

Describe the bug

In my monorepo I have a project that consists of a couple of "root" folders that require transpiling. Using the example from the minimal reproduction repo, let's say these are the folders:

  • integration-tests
  • generated
  • src
$ tree -L 2 .
.
├── generated
│   └── some-api
├── integration-tests
│   └── one.ts
├── package.json
├── src
│   ├── main.ts
│   ├── service-A
│   └── service-B
├── tsconfig-js.json
└── tsconfig.json

The goal is to transpile all 3 folders with a single command.

When you run tsc -b script in the workspace, it produces the .js output in an analogous structure of the workspace folder itself, just nested inside dist:

$ tree -L 2 dist
dist
├── generated
│   └── some-api
├── integration-tests
│   ├── one.d.ts
│   └── one.js
├── src
│   ├── main.d.ts
│   ├── main.js
│   ├── service-A
│   └── service-B
└── tsconfig.tsbuildinfo

I want to achieve the same result with swc. But it turns out that swc . -d dist does not work at all. I tried to come up with various different ways that could work. I've listed them below.

Different approaches

  • swc . -d dist

    ❌ This is the most natural way of running the command, but it does not generate any output.

  • cd ..; swc backend -d backend/dist --config-file backend/.swcrc

    ✅ Generates correct output.

  • cd ..; swc $PWD -d $PWD/dist --config-file $PWD/.swcrc

    ✅ Generates correct output.

  • swc $PWD -d $PWD/dist

    ❌ Generates a malformed (flattened) directory structure.

    $ tree -L 2 dist
    
    dist
    ├── main.js
    ├── main.js.map
    ├── one.js
    ├── one.js.map
    ├── service-A
    │   ├── a.js
    │   └── a.js.map
    ├── service-B
    │   ├── b.js
    │   └── b.js.map
    └── some-api
      ├── api.js
      └── api.js.map
    
    3 directories, 10 files
    
  • swc src -d dist/src & swc generated -d dist/generated & swc integration-tests -d dist/integration-tests

    ✅ Generates correct output.

The "outer" commands (requiring to move out of the directory) look like a hack. When working in a monorepo like this one, it is required to use the top-level swc packages, cause yarn will not let you run backend's copy of swc from an outer directory. This means that you cannot use the devDependency of a package to build it, you need to rely on external tools, which is not as portable as I'd like it to be.

The "inner" commands (those that are run inside the project's working directory) do not work correctly. The version with the dot (.) is what I'd suppose to be the correct usage of the CLI, but it does not produce any output. But even if I provide the same absolute paths as for the "outer absolute" command, it generates a different output when run inside the workspace folder.

Running 3 separate commands for each of the root folders provides a correct output, but it requires listing the folders manually. You cannot combine such script with a flag like --watch by appending the flag, because it will be only applied to the last command. You also need to update the command whenever one of the folder changes or you add another one.

Input code

No response

Config

{
    "$schema": "https://json.schemastore.org/swcrc",
    "sourceMaps": true,
    "module": {
        "type": "commonjs",
        "strict": true
    },
    "jsc": {
        "target": "es2020",
        "parser": {
            "syntax": "typescript",
            "decorators": true,
            "dynamicImport": true
        },
        "paths": {
            "@generated/*": ["generated/*"],
            "@app/*": ["src/*"]
        },
        "baseUrl": ".",
        "transform": {
            "decoratorMetadata": true
        }
    },
    "exclude": ["node_modules", "./dist"]
}

Playground link (or link to the minimal reproduction)

https://github.com/akwodkiewicz/swc-folders-bug

SWC Info output

$ npx -y swc-info@latest

Operating System:
    Platform: darwin
    Arch: arm64
    Machine Type: arm64
    Version: Darwin Kernel Version 23.0.0: Fri Sep 15 14:41:43 PDT 2023; root:xnu-10002.1.13~1/RELEASE_ARM64_T6000
    CPU: (10 cores)
        Models: Apple M1 Pro

Binaries:
    Node: 18.16.0
    npm: 9.5.1
    Yarn: 3.4.1
    pnpm: N/A

Relevant Packages:
    @swc/core: 1.3.93
    @swc/helpers: N/A
    @swc/types: 0.1.5
    typescript: 5.2.2

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

Next.js info:
    output: N/A

Expected behavior

I'd expect one (or both) to work correctly:

  • swc . -d dist

  • swc $PWD -d $PWD/dist

Actual behavior

  • swc . -d dist

    Does not generate any output

  • swc $PWD -d $PWD/dist

    Generates malformed (flattened) directory structure

Version

cli: 0.1.62, core: 1.3.93

Additional context

No response

@akwodkiewicz akwodkiewicz changed the title Cannot transpile from inside the working directory Cannot transpile the working directory itself Oct 18, 2023
@kdy1 kdy1 added this to the Planned milestone Oct 18, 2023
@kdy1 kdy1 self-assigned this Nov 15, 2023
@kdy1 kdy1 modified the milestones: Planned, v1.3.98 Nov 21, 2023
@swc-bot
Copy link
Collaborator

swc-bot commented Dec 21, 2023

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 Dec 21, 2023
dsvgit pushed a commit to dsvgit/cli that referenced this issue Jan 27, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Development

No branches or pull requests

3 participants