Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Source map sources relative to temporary output path #2218

Open
badeball opened this issue Apr 30, 2022 · 7 comments
Open

Source map sources relative to temporary output path #2218

badeball opened this issue Apr 30, 2022 · 7 comments

Comments

@badeball
Copy link

badeball commented Apr 30, 2022

Hi, Evan

Thanks for a wonderful library!

I have a question / something regarding the sources property of outputted (inline) sourcemaps. Given I have two files,

// foo.mjs
export default function foo () {
  console.log("YOLO!");
}

.. and

// index.mjs
import foo from "./foo.mjs"

foo();

.. and I build like the following

esbuild.build({
  entryPoints: ["index.mjs"],
  bundle: true,
  sourcemap: "inline"
});

.. I get a nice sources property containing ["foo.mjs", "index.mjs"]. However, this is all outputted to stdout. I'm using esbuild in part with Cypress, a testing utility, and preprocessors of Cypress are provided a temporary file location in which they are expected to output bundles. Hence, we build like

esbuild.build({
  entryPoints: ["index.mjs"],
  bundle: true,
  sourcemap: "inline",
  outfile: "/tmp/tmp-1153466-5Fh3yAWuH7tu" // <- example value
});

At that point, the sources property contains

[
  "../home/jonas/projects/esbuild-sourcemap-example/foo.mjs",
  "../home/jonas/projects/esbuild-sourcemap-example/index.mjs"
]

.. and it becomes impossible to use the sources and determine file locations programmatically without also knowing the build-time CWD and the temporary output directory.

Is there a way to build to a location and retain "nice" and library-friendly values for sources?

Regards

Jonas

@badeball badeball changed the title Source map sources related to temporary output path Source map sources relative to temporary output path Apr 30, 2022
@badeball
Copy link
Author

FYI, I tried looking into the sourceRoot configuration property, but that didn't appear to changes the sources, but merely append said property to the sourcemap.

@jacobduba
Copy link

jacobduba commented Jul 14, 2022

I am running into the same issue with AWS's SAM CLI. It copies the files to a folder inside /tmp/ before running esbuild. The source maps then end up looking like ../../../../../../../tmp/tmpqnj0qy53/app.ts so line by line debugging does not work. I think the best way to approach this would to have an entryPointSourceMap option that tells esbuild where to point the source maps at.

@Lilja
Copy link

Lilja commented Jan 19, 2023

I've also happened to experience the same issue. It feels weird that we can't control this prefix since it's wrong when building into a dist directory and the source files are pointing to ../src/<whatever>.

Are there any workarounds to this?

@connorjclark
Copy link

A workaround is to use outdir instead of outfile, set write: false, and manually write the results yourself. The map you get will have sources relative to whatever you set for outdir.

@jasonterando
Copy link

I am running into the same issue with AWS's SAM CLI. It copies the files to a folder inside /tmp/ before running esbuild. The source maps then end up looking like ../../../../../../../tmp/tmpqnj0qy53/app.ts so line by line debugging does not work. I think the best way to approach this would to have an entryPointSourceMap option that tells esbuild where to point the source maps at.

@jacobduba - did you ever find a workaround that worked with AWS SAM's integrated calls of esbuild?

@jacobduba
Copy link

@jasonterando Since AWS SAM moved the code to a tmp directory, I submitted a PR to not move the code to a tmp directory. It fixed the issue but broke some other stuff so the maintainers rejected it.

@jasonterando
Copy link

@jacobduba thanks for the reply. For now, I'll stick with calling esbuild from package.json, using nodemon to rebuild upon changes. For AWS SAM and VSCode users, I got the debugger working by configuring launch.json as follows:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Debug (Attach)",
            "request": "attach",
            "address": "localhost",
            "skipFiles": [
                "<node_internals>/**",
                "/var/runtime/*",
                "node_modules/**"
            ],
            "type": "node",
            "port": 5858,
            "remoteRoot": "/var/task",
            "localRoot": "${workspaceFolder}/.aws-sam/build/LambdaApi",
            "sourceMaps": true,
            "sourceMapPathOverrides": {
                "../node_modules/*": "${workspaceFolder}/node_modules/*",
                "../src/*": "${workspaceFolder}/src/*",
            }
        }
    ]
}

# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants