Skip to content

Bundle mode with sourceMapTraceback does not replace {#SourceMapTraceback} #1109

Description

@Toliak

Description

Enabling bundle-mode (luaBundle and luaBundleEntry) with sourceMapTraceback) will cause to appear {#SourceMapTraceback} in the result file.

Steps to reproduce

  1. Create a project with dependencies:
{
  "typescript": "^4.4.2",
  "typescript-to-lua": "^1.0.0"
}
  1. Create tsconfig.json with listed content:
{
  "$schema": "https://raw.githubusercontent.com/TypeScriptToLua/vscode-typescript-to-lua/master/tsconfig-schema.json",
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "strict": true,
    "skipLibCheck": true
  },
  "tstl": {
    "luaTarget": "JIT",
    "sourceMapTraceback": true,
    "luaBundleEntry": "src/index.ts",
    "luaBundle": "dist/index.lua"
  }
}
  1. Create an empty src/index.ts file
  2. Run tstl
  3. Check the received dist/index.lua. It looks like:
--[[ Generated with https://github.com/TypeScriptToLua/TypeScriptToLua ]]

local ____modules = {}
local ____moduleCache = {}
local ____originalRequire = require
local function require(file)
    --- require code here, ok .....
end
____modules = {
["lualib_bundle"] = function() 
    --- lualib code here, ok .....
end,
["index"] = function() --[[ Generated with https://github.com/TypeScriptToLua/TypeScriptToLua ]]
require("lualib_bundle");
{#SourceMapTraceback}     -- <<--- Look here
 end,
}
return require("index")

Why does it happen

The "macro-string" {#SourceMapTraceback} should be replaced here:

if (this.options.sourceMapTraceback) {
const stackTraceOverride = this.printStackTraceOverride(rootSourceNode);
code = code.replace("{#SourceMapTraceback}", stackTraceOverride);
}

Debugging

I will try to debug the compiler and list the ideas below. I believe it will help to determine the problem,

  1. Debugger says, that print function from LuaPrinter.ts successfully replaces the macro-string
  2. Correct version is being added into the transpiledFiles array
    const processSourceFile = (sourceFile: ts.SourceFile) => {
    const { file, diagnostics: transformDiagnostics } = transformSourceFile(program, sourceFile, visitorMap);
    diagnostics.push(...transformDiagnostics);
    if (!options.noEmit && !options.emitDeclarationOnly) {
    const printResult = printer(program, emitHost, sourceFile.fileName, file);
    transpiledFiles.push({
    sourceFiles: [sourceFile],
    fileName: path.normalize(sourceFile.fileName),
    luaAst: file,
    ...printResult,
    });
    }
  3. I think error is in this line (especially, in getEmitPlan function). Because freshFiles contains correct code (with replaced macro-string)
    const { emitPlan } = this.getEmitPlan(program, diagnostics, freshFiles);
  4. On this line: resolutionResult.resolvedFiles contains correct code, bundleFile -- does not
    const [bundleDiagnostics, bundleFile] = getBundleResult(program, resolutionResult.resolvedFiles);
  5. Here. The moduleSourceNode function. Returns array of strings for lualib_bundle and array with incorrect SourceNode for index
    const moduleTableEntries = files.map(f => moduleSourceNode(f, createModulePath(f.fileName, program)));

    image
  6. Finally, this line. The function moduleSourceNode contains code (with correct code) and sourceMapNode with child with incorrect code.
    return joinSourceChunks([tableEntryHead, sourceMapNode ?? code, tableEntryTail]);

Suggestion

I have replaced the line (p. 6) by return joinSourceChunks([tableEntryHead, code, tableEntryTail]); and it works.

Moreover, here specified, that code always exists. So, what about to put code instead of sourceMapNode ?? code?

interface BaseFile {
code: string;
sourceMap?: string;
sourceFiles?: ts.SourceFile[];
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugscope: bundlingRelated to bundling of generated lua files.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions