swc icon indicating copy to clipboard operation
swc copied to clipboard

`cli`: Pathing under --out-dir has extra segments

Open alexeagle opened this issue 3 years ago • 8 comments

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

alexeagle avatar Dec 13 '21 06:12 alexeagle

I also encountered the same problem

EastblueOkay avatar Dec 22 '21 08:12 EastblueOkay

Can I work on this, @kdy1?

ruiconti avatar Jan 06 '22 13:01 ruiconti

Of course, thank you!

kdy1 avatar Jan 06 '22 13:01 kdy1

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.

reintroducing avatar Mar 31 '22 15:03 reintroducing

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.

milovangudelj avatar Aug 22 '22 12:08 milovangudelj

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'

meeroslav avatar Aug 11 '23 15:08 meeroslav

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

dinhero21 avatar Jan 27 '24 03:01 dinhero21

@Dinhero21 Please see https://github.com/swc-project/cli/issues/281

kdy1 avatar Jan 27 '24 03:01 kdy1