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

cli: Pathing under --out-dir has extra segments #3028

Open
alexeagle opened this issue Dec 13, 2021 · 8 comments
Open

cli: Pathing under --out-dir has extra segments #3028

alexeagle opened this issue Dec 13, 2021 · 8 comments
Labels

Comments

@alexeagle
Copy link
Contributor

Describe the bug

When the input directory has multiple segments, swc appears to strip only the first. Remaining segments appear in the output tree, making it more nested than expected.

Example:

alexeagle@system76-pc:~/repros/swc-paths$ mkdir -p some/path/to/dir
alexeagle@system76-pc:~/repros/swc-paths$ cat > some/path/to/dir/file.js
const a = 1

alexeagle@system76-pc:~/repros/swc-paths$ npx swc some/path/to/dir --out-dir=some/other/path
Successfully compiled: 1 file with swc (8.46ms)
alexeagle@system76-pc:~/repros/swc-paths$ ls -R some/other/path
some/other/path:
path

some/other/path/path:
to

some/other/path/path/to:
dir

some/other/path/path/to/dir:
file.js

I don't think we expected the repeated path/path part of that resulting output directory.

Note, under bazel this results in output directories like bazel-bin/examples/directory/minify/k8-fastbuild/bin/examples/directory/split_app/file1.js

Input code

No response

Config

No response

Playground link

No response

Expected behavior

I would have expected that example to write output to some/other/path/file.js

Version

@swc/cli: 0.1.53 @swc/core: 1.2.119

Additional context

No response

@EastblueOkay
Copy link

I also encountered the same problem

@ruiconti
Copy link

ruiconti commented Jan 6, 2022

Can I work on this, @kdy1?

@kdy1
Copy link
Member

kdy1 commented Jan 6, 2022

Of course, thank you!

@kdy1 kdy1 changed the title Pathing under --out-dir has extra segments cli: Pathing under --out-dir has extra segments Mar 12, 2022
@kdy1 kdy1 changed the title cli: Pathing under --out-dir has extra segments cli: Pathing under --out-dir has extra segments Mar 12, 2022
@reintroducing
Copy link

Just ran into this as well. The expected output is consistent with how Babel handles the same command but the extra directories are definitely being generated unexpectedly. @ruiconti @kdy1 has there been any headway on this one? i feel like this is a pretty large issue and causes extra code to have to be written to re-copy all the output into the expected dir and remove the extra generated dirs manually.

@milovangudelj
Copy link

I think I have the same issue.

I'm building a component library for one of my projects and I have the component files under ./src/components. When running npx swc ./src/components -d ./dist I expect all files to be compiled and placed in the destination folder as tsc would do, instead I get an extra components folder inside the output dist folder.

As a temporary fix I'm compiling everything in the root directory and then renaming the output components directory to dist. See build:js below

// package.json

{
  ... stuff ...
  "sripts": {
    ... other stuff ...
    "build": "yarn clean && yarn build:js && yarn build:types",
    "build:js": "npx swc ./src/components -d ./ && mv ./components ./dist",
    "build:types": "tsc --emitDeclarationOnly --declaration --outDir ./dist",
    "clean": "rm -rf ./dist"
  },
  ... more stuff ...
}

build:types is there because, as far as I know, swc doesn't have an option for generating type declaration files.

@meeroslav
Copy link

We are using this logic to create a workaround at the moment:

  const firstSegmentIndex = srcPath.indexOf('/');
  if (firstSegmentIndex !== -1) {
    // TODO remove this when https://github.com/swc-project/swc/issues/3028 is fixed
    destPath = destPath.slice(0, -srcPath.length + firstSegmentIndex);
  }

Which changes this:

srcPath: 'mylib/nested/other-nested'
destPath: 'dist/mylib/nested/other-nested'

to

srcPath: 'mylib/nested/other-nested'
destPath: 'dist/mylib'

@Dinhero21
Copy link

Dinhero21 commented Jan 27, 2024

swc src -d dist is transpiling from ./src to ./dist/src instead of ./dist, this wasn't an issue with older versions

Fine:
@swc/cli: ^0.1.65
@swc/core: ^1.3.105

Extra segments:
@swc/cli: ^0.3.2
@swc/core: ^1.3.106

@kdy1
Copy link
Member

kdy1 commented Jan 27, 2024

@Dinhero21 Please see swc-project/cli#281

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

8 participants