swc icon indicating copy to clipboard operation
swc copied to clipboard

Multiline comments breaks debugger breakpoints

Open Kaizer69 opened this issue 2 years ago • 2 comments

Describe the bug

When you have a file with a multicomment like

/** I like to comment a multiline */

or

/* I like to comment a multiline */

the transpiled file breaks sourcemap position. Infact if you remove the comments from the transpiled file, the sourcemaps works correctly. Tested with jsc.preserveAllComments true/false

Input code

/* eslint-disable @typescript-eslint/no-var-requires */
/**
 * This module executes some code for test
 * Running `npm run build` or `npm run build:main` for build that
 */
import path from 'node:path';

console.log("Testing log");

console.log("Breakpoint fail line here");

console.log("And here");

console.log("Ecc ecc");

Config

{
    "sourceMaps": true,
    "inlineSourcesContent": false,
    "minify": false,
    "jsc": {
        "parser": {
            "target": "es2021",
            "syntax": "typescript",
            "jsx": true,
            "tsx": true,
            "dynamicImport": true,
            "allowJs": true,
            "decorators": true,
            "topLevelAwait": true,
            "experimental": {
                "keepImportAssertions": true
            }
        },
    "preserveAllComments": false,
    "transform": null,
    "target": "es5",
    "loose": false,
    "externalHelpers": true,
    "keepClassNames": true
  },
  "module": {
    "type": "commonjs",
    "strict": false,
    "strictMode": true,
    "lazy": false,
    "noInterop": false
  }
}

Playground link

No response

Expected behavior

SWC need to strip out comments with /** */ or /**/

Actual behavior

No response

Version

1.2.242

Additional context

Node 18 Typescript (CommonJS) Webstorm 2022

Kaizer69 avatar Aug 25 '22 19:08 Kaizer69

Well this issue was not fun to track down, the issue is still present/also happens with: SWC version @swc/[email protected] + Node 18.x w/ --enable-source-maps + TypeScript ESM

Important additional information

It has been discovered at https://github.com/vercel/next.js/issues/32470 that this issue only happens when the file uses CRLF (Windows) newlines, and does not occur when using LF (Unix) newlines. This observation is not specific to Next.js and I can reproduce it on standalone @swc/core.

Furthermore, an additional possibly related issue, jsc.preserveAllComments = false seems to be entirely ignored and all comments preserved if minify = false, and setting minify = true together with jsc.preserveAllComments = false fixes the original problem in this issue since the CRLF multiline comments will be actually stripped away now.

Update 1

The workaround is not perfect, it seems swc still decides to preserve some comments even with minify = true and jsc.preserveAllComments = false, namely JSDoc comments with a @license field, there may be others, and those cause the problem here to resurface if using CRLF newlines.

Update 2

As a final workaround to deal with Update 1, I am now passing my input through crlf-normalize to forcefully set all newlines to LF before passing it to swc and its been reliable so far, but of course having to normalize the newlines of large scripts will come at a performance cost so its still not a perfect solution and only a workaround.

jhmaster2000 avatar Oct 13 '22 13:10 jhmaster2000

@jhmaster2000 Is there a repro? I'm not sure how can I reproduce this issue. Either of next.js repro or standalone swc repro would be awesome

kdy1 avatar Jan 30 '24 06:01 kdy1