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

absolute paths causes error on windows OS #4730

Closed
aelbore opened this issue May 21, 2022 · 13 comments · Fixed by #4739
Closed

absolute paths causes error on windows OS #4730

aelbore opened this issue May 21, 2022 · 13 comments · Fixed by #4739
Assignees
Milestone

Comments

@aelbore
Copy link

aelbore commented May 21, 2022

Describe the bug

when the paths are full path or absolute path it causes error when running in Windows
Note: it works on MacOSX

transformSync and bundle has same behavior

Input code

./src/index.ts

    import { displayB } from '@print/b'

    async function display() {
      const displayA = await import('@print/a').then(c => c.displayA)
      console.log(displayA())
      console.log(displayB())
    }

./packages/a/src/index.s

      export function displayA() {
        return 'Display A'
      }   

./packages/b/src/index.s

      export function displayB() {
        return 'Display B'
      }    

transform.ts

import { bundle } from "@swc/core"
import { join } from 'path'

(async function() {

  const result = await bundle({
    entry: {
      web: './src/index.ts'
    },
    output: {
      name: 'my-bundle',
      path: './dist'
    },
    options: {
      "jsc": {
        "parser": {
          "syntax": "typescript",
          "dynamicImport": true
        },
        "target": "es2020",
        "paths": {
          "@print/a": [ 
            join(process.cwd(), "./packages/a/src/index.ts")
          ],
          "@print/b": [ 
            join(process.cwd(), "./packages/b/src/index.ts")
          ]
        }
      },
      isModule: true
    },
    module: {}
  })

  console.log(result['web'].code)

})() 


### Config

_No response_

### Playground link

_No response_

### Expected behavior

It should work on Windows machine

### Actual behavior

_No response_

### Version

1.2.187

### Additional context

_No response_
@aelbore aelbore added the C-bug label May 21, 2022
@aelbore
Copy link
Author

aelbore commented May 21, 2022

adding baseUrl = process.cwd() with paths that has relative path has same issue
im working on a project that baseUrl is dynamic which means anyone can set it

@kdy1 kdy1 added this to the Planned milestone May 21, 2022
@kdy1 kdy1 assigned kdy1 and unassigned kdy1 May 21, 2022
@kdy1 kdy1 removed this from the Planned milestone May 21, 2022
@kdy1
Copy link
Member

kdy1 commented May 21, 2022

Can you share more info, like the config to transform() (not bundle(), as it's useless

@aelbore
Copy link
Author

aelbore commented May 22, 2022

@kdy1 you can use same config above with transform
where process.cwd() is C:\dev or any dynamic path
since i dont have baseUrl in the config i expected it will not be use (this is what i understand)

import { transformSync } from '@swc/core'
import { join } from 'path'

const content = `
import { displayB } from '@print/b'

async function display() {
  const displayA = await import('@print/a').then(c => c.displayA)
  console.log(displayA())
  console.log(displayB())
}

display()
`

const { code } = transformSync(content, {
  "jsc": {
    "parser": {
      "syntax": "typescript",
      "dynamicImport": true
    },
    "target": "es2020",
    "paths": {
      "@print/a": [ 
        join(process.cwd(), "./packages/a/src/index.ts")
      ],
      "@print/b": [ 
        join(process.cwd(), "./packages/b/src/index.ts")
      ]
    }
  },
  "module": {
    "type": "commonjs"
  }
})

console.log(code)

Output

"use strict";
var _b = require("C:\\dev\\packages\\b\\src\\index.ts");
function _getRequireWildcardCache() {
    if (typeof WeakMap !== "function") return null;
    var cache = new WeakMap();
    _getRequireWildcardCache = function() {
        return cache;
    };
    return cache;
}
function _interopRequireWildcard(obj) {
    if (obj && obj.__esModule) {
        return obj;
    }
    if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
        return {
            default: obj
        };
    }
    var cache = _getRequireWildcardCache();
    if (cache && cache.has(obj)) {
        return cache.get(obj);
    }
    var newObj = {};
    var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
    for(var key in obj){
        if (Object.prototype.hasOwnProperty.call(obj, key)) {
            var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
            if (desc && (desc.get || desc.set)) {
                Object.defineProperty(newObj, key, desc);
            } else {
                newObj[key] = obj[key];
            }
        }
    }
    newObj.default = obj;
    if (cache) {
        cache.set(obj, newObj);
    }
    return newObj;
}
async function display() {
    const displayA = await Promise.resolve().then(function() {
        return _interopRequireWildcard(require("C:\\dev\\packages\\a\\src\\index.ts"));
    }).then((c)=>c.displayA);
    console.log(displayA());
    console.log((0, _b).displayB());
}
display();

Expected

"use strict";
var _b = require("C:\\dev\\packages\\b\\src\\index.ts");
function _getRequireWildcardCache() {
    if (typeof WeakMap !== "function") return null;
    var cache = new WeakMap();
    _getRequireWildcardCache = function() {
        return cache;
    };
    return cache;
}
function _interopRequireWildcard(obj) {
    if (obj && obj.__esModule) {
        return obj;
    }
    if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
        return {
            default: obj
        };
    }
    var cache = _getRequireWildcardCache();
    if (cache && cache.has(obj)) {
        return cache.get(obj);
    }
    var newObj = {};
    var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
    for(var key in obj){
        if (Object.prototype.hasOwnProperty.call(obj, key)) {
            var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
            if (desc && (desc.get || desc.set)) {
                Object.defineProperty(newObj, key, desc);
            } else {
                newObj[key] = obj[key];
            }
        }
    }
    newObj.default = obj;
    if (cache) {
        cache.set(obj, newObj);
    }
    return newObj;
}
async function display() {
    const displayA = await Promise.resolve().then(function() {
        return _interopRequireWildcard(require("C:\\dev\\packages\\a\\src\\index.ts"));
    }).then((c)=>c.displayA);
    console.log(displayA());
    console.log((0, _b).displayB());
}
display();

@kdy1 kdy1 added this to the Planned milestone May 22, 2022
@kdy1 kdy1 self-assigned this May 22, 2022
@kdy1
Copy link
Member

kdy1 commented May 22, 2022

Seems like you make a mistake while copy-pasting expected output or actual output?
Those seem identical

@aelbore
Copy link
Author

aelbore commented May 22, 2022

if you notice the Expected has absolute path compare to Output

@kdy1
Copy link
Member

kdy1 commented May 22, 2022

I searched by copying the path and it was identical

@kdy1
Copy link
Member

kdy1 commented May 22, 2022

Oh your config is wrong.
jsc.paths requires jsc.baseUrl

@kdy1 kdy1 closed this as not planned Won't fix, can't repro, duplicate, stale May 22, 2022
@kdy1 kdy1 removed this from the Planned milestone May 22, 2022
@aelbore
Copy link
Author

aelbore commented May 22, 2022

Oh your config is wrong. jsc.paths requires jsc.baseUrl

yes adding baseUrl same issue.

@aelbore
Copy link
Author

aelbore commented May 22, 2022

or maybe i missed how does the paths works with baseUrl

@aelbore
Copy link
Author

aelbore commented May 22, 2022

anyway thanks for looking into it, i understand if this is not a priority, by the way thanks for swc i heavily using it on my own project

@aelbore
Copy link
Author

aelbore commented May 22, 2022

as i checked baseUrl is optional if i didnt put any baseUrl what is the value of baseUrl?

Screenshot 2022-05-22 at 11 59 39 PM

@kdy1 kdy1 added this to the Planned milestone May 22, 2022
@kdy1 kdy1 reopened this May 22, 2022
@kdy1
Copy link
Member

kdy1 commented May 22, 2022

I don't remember.
Consult the typescript docs for paths

@kdy1 kdy1 modified the milestones: Planned, v1.2.190 May 23, 2022
@swc-bot
Copy link
Collaborator

swc-bot commented Oct 16, 2022

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 Oct 16, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Development

Successfully merging a pull request may close this issue.

3 participants