diff --git a/lib/internal/main/embedding.js b/lib/internal/main/embedding.js index 93aa8bb38a06a6..105f4a7b6da777 100644 --- a/lib/internal/main/embedding.js +++ b/lib/internal/main/embedding.js @@ -56,6 +56,7 @@ function embedderRunCjs(content) { function: compiledWrapper, cachedDataRejected, sourceMapURL, + sourceURL, } = compileFunctionForCJSLoader( content, filename, @@ -69,7 +70,7 @@ function embedderRunCjs(content) { content, customModule, false, // isGeneratedSource - undefined, // sourceURL, TODO(joyeecheung): should be extracted by V8 + sourceURL, sourceMapURL, ); } diff --git a/lib/internal/modules/cjs/loader.js b/lib/internal/modules/cjs/loader.js index 6437b3ab218a71..9eac7ab126f302 100644 --- a/lib/internal/modules/cjs/loader.js +++ b/lib/internal/modules/cjs/loader.js @@ -1640,9 +1640,9 @@ function wrapSafe(filename, content, cjsModuleInstance, format) { ); // Cache the source map for the module if present. - const { sourceMapURL } = script; + const { sourceMapURL, sourceURL } = script; if (sourceMapURL) { - maybeCacheSourceMap(filename, content, cjsModuleInstance, false, undefined, sourceMapURL); + maybeCacheSourceMap(filename, content, cjsModuleInstance, false, sourceURL, sourceMapURL); } return { @@ -1667,7 +1667,7 @@ function wrapSafe(filename, content, cjsModuleInstance, format) { // Cache the source map for the module if present. if (result.sourceMapURL) { - maybeCacheSourceMap(filename, content, cjsModuleInstance, false, undefined, result.sourceMapURL); + maybeCacheSourceMap(filename, content, cjsModuleInstance, false, result.sourceURL, result.sourceMapURL); } return result; diff --git a/lib/internal/modules/esm/translators.js b/lib/internal/modules/esm/translators.js index 0a8e4c9e5d3af5..00ee65270eeeb7 100644 --- a/lib/internal/modules/esm/translators.js +++ b/lib/internal/modules/esm/translators.js @@ -121,10 +121,10 @@ translators.set('module', function moduleStrategy(url, source, isMain) { function loadCJSModule(module, source, url, filename, isMain) { const compileResult = compileFunctionForCJSLoader(source, filename, false /* is_sea_main */, false); - const { function: compiledWrapper, sourceMapURL } = compileResult; + const { function: compiledWrapper, sourceMapURL, sourceURL } = compileResult; // Cache the source map for the cjs module if present. if (sourceMapURL) { - maybeCacheSourceMap(url, source, module, false, undefined, sourceMapURL); + maybeCacheSourceMap(url, source, module, false, sourceURL, sourceMapURL); } const cascadedLoader = require('internal/modules/esm/loader').getOrInitializeCascadedLoader(); diff --git a/lib/internal/modules/esm/utils.js b/lib/internal/modules/esm/utils.js index 3c925cb0eea901..01dd16ed655e7f 100644 --- a/lib/internal/modules/esm/utils.js +++ b/lib/internal/modules/esm/utils.js @@ -353,7 +353,7 @@ function compileSourceTextModule(url, source, cascadedLoader) { } // Cache the source map for the module if present. if (wrap.sourceMapURL) { - maybeCacheSourceMap(url, source, wrap, false, undefined, wrap.sourceMapURL); + maybeCacheSourceMap(url, source, wrap, false, wrap.sourceURL, wrap.sourceMapURL); } return wrap; } diff --git a/lib/internal/source_map/source_map_cache.js b/lib/internal/source_map/source_map_cache.js index 639051ec10fe61..5ebf07d876956c 100644 --- a/lib/internal/source_map/source_map_cache.js +++ b/lib/internal/source_map/source_map_cache.js @@ -139,7 +139,9 @@ function extractSourceMapURLMagicComment(content) { } /** - * Caches the source map if it is present in the content, with the given filename, moduleInstance, and sourceURL. + * Caches the source map, with the given filename, moduleInstance, sourceURL and sourceMapURL. + * This function does not automatically extract the source map from the content. The caller should either + * extract the source map from the content via V8 API or use {@link extractSourceURLMagicComment} explicitly. * @param {string} filename - the actual filename * @param {string} content - the actual source content * @param {import('internal/modules/cjs/loader').Module | ModuleWrap} moduleInstance - a module instance that @@ -162,20 +164,13 @@ function maybeCacheSourceMap(filename, content, moduleInstance, isGeneratedSourc return; } - if (sourceMapURL === undefined) { - sourceMapURL = extractSourceMapURLMagicComment(content); - } - // Bail out when there is no source map url. if (typeof sourceMapURL !== 'string') { return; } - // FIXME: callers should obtain sourceURL from v8 and pass it - // rather than leaving it undefined and extract by regex. - if (sourceURL === undefined) { - sourceURL = extractSourceURLMagicComment(content); - } + // Normalize the sourceURL to a file URL if it is a path. + sourceURL = normalizeReferrerURL(sourceURL); const data = dataFromUrl(filename, sourceMapURL); // `data` could be null if the source map is invalid. @@ -192,9 +187,6 @@ function maybeCacheSourceMap(filename, content, moduleInstance, isGeneratedSourc if (isGeneratedSource) { generatedSourceMapCache.set(filename, entry); - if (sourceURL) { - generatedSourceMapCache.set(sourceURL, entry); - } return; } // If it is not a generated source, we assume we are in a "cjs/esm" @@ -215,8 +207,14 @@ function maybeCacheGeneratedSourceMap(content) { if (sourceURL === null) { return; } + const sourceMapURL = extractSourceMapURLMagicComment(content); + if (sourceMapURL === null) { + return; + } + try { - maybeCacheSourceMap(sourceURL, content, null, true, sourceURL); + // Use the sourceURL as the filename, and do not create a duplicate entry. + maybeCacheSourceMap(sourceURL, content, null, true, undefined /** no duplicated sourceURL */, sourceMapURL); } catch (err) { // This can happen if the filename is not a valid URL. // If we fail to cache the source map, we should not fail the whole process. diff --git a/src/env_properties.h b/src/env_properties.h index 5c90bfd8cd7948..8852d83eda3c3f 100644 --- a/src/env_properties.h +++ b/src/env_properties.h @@ -363,6 +363,7 @@ V(sni_context_string, "sni_context") \ V(source_string, "source") \ V(source_map_url_string, "sourceMapURL") \ + V(source_url_string, "sourceURL") \ V(specifier_string, "specifier") \ V(stack_string, "stack") \ V(standard_name_string, "standardName") \ diff --git a/src/module_wrap.cc b/src/module_wrap.cc index 2290a9072bfcfb..dd0f74c5580561 100644 --- a/src/module_wrap.cc +++ b/src/module_wrap.cc @@ -308,6 +308,13 @@ void ModuleWrap::New(const FunctionCallbackInfo& args) { return; } + if (that->Set(context, + realm->env()->source_url_string(), + module->GetUnboundModuleScript()->GetSourceURL()) + .IsNothing()) { + return; + } + if (that->Set(context, realm->env()->source_map_url_string(), module->GetUnboundModuleScript()->GetSourceMappingURL()) diff --git a/src/node_contextify.cc b/src/node_contextify.cc index 6d13a62b7feb7d..dbf5eadb6bf2db 100644 --- a/src/node_contextify.cc +++ b/src/node_contextify.cc @@ -1109,6 +1109,13 @@ void ContextifyScript::New(const FunctionCallbackInfo& args) { return; } + if (args.This() + ->Set(env->context(), + env->source_url_string(), + v8_script->GetSourceURL()) + .IsNothing()) + return; + if (args.This() ->Set(env->context(), env->source_map_url_string(), @@ -1575,12 +1582,23 @@ MaybeLocal ContextifyFunction::CompileFunctionAndCacheResult( Local result = Object::New(isolate); if (result->Set(parsing_context, env->function_string(), fn).IsNothing()) return {}; + + // ScriptOrigin::ResourceName() returns SourceURL magic comment content if + // present. + if (result + ->Set(parsing_context, + env->source_url_string(), + fn->GetScriptOrigin().ResourceName()) + .IsNothing()) { + return {}; + } if (result ->Set(parsing_context, env->source_map_url_string(), fn->GetScriptOrigin().SourceMapUrl()) - .IsNothing()) + .IsNothing()) { return {}; + } std::unique_ptr new_cached_data; if (produce_cached_data) { @@ -1818,12 +1836,16 @@ static void CompileFunctionForCJSLoader( Local names[] = { env->cached_data_rejected_string(), env->source_map_url_string(), + env->source_url_string(), env->function_string(), FIXED_ONE_BYTE_STRING(isolate, "canParseAsESM"), }; Local values[] = { Boolean::New(isolate, cache_rejected), fn.IsEmpty() ? undefined : fn->GetScriptOrigin().SourceMapUrl(), + // ScriptOrigin::ResourceName() returns SourceURL magic comment content if + // present. + fn.IsEmpty() ? undefined : fn->GetScriptOrigin().ResourceName(), fn.IsEmpty() ? undefined : fn.As(), Boolean::New(isolate, can_parse_as_esm), }; diff --git a/test/common/assertSnapshot.js b/test/common/assertSnapshot.js index dbd4a6079861fc..7a40c94389eda9 100644 --- a/test/common/assertSnapshot.js +++ b/test/common/assertSnapshot.js @@ -16,6 +16,10 @@ function replaceStackTrace(str, replacement = '$1*$7$8\n') { return str.replace(stackFramesRegexp, replacement); } +function replaceInternalStackTrace(str) { + return str.replaceAll(/(\W+).*node:internal.*/g, '$1*'); +} + function replaceWindowsLineEndings(str) { return str.replace(windowNewlineRegexp, ''); } @@ -24,8 +28,11 @@ function replaceWindowsPaths(str) { return common.isWindows ? str.replaceAll(path.win32.sep, path.posix.sep) : str; } -function replaceFullPaths(str) { - return str.replaceAll('\\\'', "'").replaceAll(path.resolve(__dirname, '../..'), ''); +function transformProjectRoot(replacement = '') { + const projectRoot = path.resolve(__dirname, '../..'); + return (str) => { + return str.replaceAll('\\\'', "'").replaceAll(projectRoot, replacement); + }; } function transform(...args) { @@ -94,11 +101,12 @@ async function spawnAndAssert(filename, transform = (x) => x, { tty = false, ... module.exports = { assertSnapshot, getSnapshotPath, - replaceFullPaths, replaceNodeVersion, replaceStackTrace, + replaceInternalStackTrace, replaceWindowsLineEndings, replaceWindowsPaths, spawnAndAssert, transform, + transformProjectRoot, }; diff --git a/test/fixtures/source-map/output/source_map_disabled_by_api.snapshot b/test/fixtures/source-map/output/source_map_disabled_by_api.snapshot index 655cd6695e1116..f538904873393e 100644 --- a/test/fixtures/source-map/output/source_map_disabled_by_api.snapshot +++ b/test/fixtures/source-map/output/source_map_disabled_by_api.snapshot @@ -1,12 +1,12 @@ Error: an error! - at functionD (*enclosing-call-site-min.js:1:156) - at functionC (*enclosing-call-site-min.js:1:97) - at functionB (*enclosing-call-site-min.js:1:60) - at functionA (*enclosing-call-site-min.js:1:26) - at Object. (*enclosing-call-site-min.js:1:199) + at functionD (*/test/fixtures/source-map/enclosing-call-site-min.js:1:156) + at functionC (*/test/fixtures/source-map/enclosing-call-site-min.js:1:97) + at functionB (*/test/fixtures/source-map/enclosing-call-site-min.js:1:60) + at functionA (*/test/fixtures/source-map/enclosing-call-site-min.js:1:26) + at Object. (*/test/fixtures/source-map/enclosing-call-site-min.js:1:199) Error: an error! - at functionD (*enclosing-call-site.js:16:17) - at functionC (*enclosing-call-site.js:10:3) - at functionB (*enclosing-call-site.js:6:3) - at functionA (*enclosing-call-site.js:2:3) - at Object. (*enclosing-call-site.js:24:3) + at functionD (*/test/fixtures/source-map/enclosing-call-site.js:16:17) + at functionC (*/test/fixtures/source-map/enclosing-call-site.js:10:3) + at functionB (*/test/fixtures/source-map/enclosing-call-site.js:6:3) + at functionA (*/test/fixtures/source-map/enclosing-call-site.js:2:3) + at Object. (*/test/fixtures/source-map/enclosing-call-site.js:24:3) diff --git a/test/fixtures/source-map/output/source_map_disabled_by_process_api.snapshot b/test/fixtures/source-map/output/source_map_disabled_by_process_api.snapshot index 655cd6695e1116..f538904873393e 100644 --- a/test/fixtures/source-map/output/source_map_disabled_by_process_api.snapshot +++ b/test/fixtures/source-map/output/source_map_disabled_by_process_api.snapshot @@ -1,12 +1,12 @@ Error: an error! - at functionD (*enclosing-call-site-min.js:1:156) - at functionC (*enclosing-call-site-min.js:1:97) - at functionB (*enclosing-call-site-min.js:1:60) - at functionA (*enclosing-call-site-min.js:1:26) - at Object. (*enclosing-call-site-min.js:1:199) + at functionD (*/test/fixtures/source-map/enclosing-call-site-min.js:1:156) + at functionC (*/test/fixtures/source-map/enclosing-call-site-min.js:1:97) + at functionB (*/test/fixtures/source-map/enclosing-call-site-min.js:1:60) + at functionA (*/test/fixtures/source-map/enclosing-call-site-min.js:1:26) + at Object. (*/test/fixtures/source-map/enclosing-call-site-min.js:1:199) Error: an error! - at functionD (*enclosing-call-site.js:16:17) - at functionC (*enclosing-call-site.js:10:3) - at functionB (*enclosing-call-site.js:6:3) - at functionA (*enclosing-call-site.js:2:3) - at Object. (*enclosing-call-site.js:24:3) + at functionD (*/test/fixtures/source-map/enclosing-call-site.js:16:17) + at functionC (*/test/fixtures/source-map/enclosing-call-site.js:10:3) + at functionB (*/test/fixtures/source-map/enclosing-call-site.js:6:3) + at functionA (*/test/fixtures/source-map/enclosing-call-site.js:2:3) + at Object. (*/test/fixtures/source-map/enclosing-call-site.js:24:3) diff --git a/test/fixtures/source-map/output/source_map_enabled_by_api.snapshot b/test/fixtures/source-map/output/source_map_enabled_by_api.snapshot index 082b3f310ed4f9..9fe43a29865b35 100644 --- a/test/fixtures/source-map/output/source_map_enabled_by_api.snapshot +++ b/test/fixtures/source-map/output/source_map_enabled_by_api.snapshot @@ -1,12 +1,12 @@ Error: an error! - at functionD (*enclosing-call-site.js:16:17) - at functionC (*enclosing-call-site.js:10:3) - at functionB (*enclosing-call-site.js:6:3) - at functionA (*enclosing-call-site.js:2:3) - at Object. (*enclosing-call-site.js:24:3) + at functionD (*/test/fixtures/source-map/enclosing-call-site.js:16:17) + at functionC (*/test/fixtures/source-map/enclosing-call-site.js:10:3) + at functionB (*/test/fixtures/source-map/enclosing-call-site.js:6:3) + at functionA (*/test/fixtures/source-map/enclosing-call-site.js:2:3) + at Object. (*/test/fixtures/source-map/enclosing-call-site.js:24:3) Error: an error! - at functionD (*enclosing-call-site-min.js:1:156) - at functionC (*enclosing-call-site-min.js:1:97) - at functionB (*enclosing-call-site-min.js:1:60) - at functionA (*enclosing-call-site-min.js:1:26) - at Object. (*enclosing-call-site-min.js:1:199) + at functionD (*/test/fixtures/source-map/enclosing-call-site-min.js:1:156) + at functionC (*/test/fixtures/source-map/enclosing-call-site-min.js:1:97) + at functionB (*/test/fixtures/source-map/enclosing-call-site-min.js:1:60) + at functionA (*/test/fixtures/source-map/enclosing-call-site-min.js:1:26) + at Object. (*/test/fixtures/source-map/enclosing-call-site-min.js:1:199) diff --git a/test/fixtures/source-map/output/source_map_enabled_by_api_node_modules.snapshot b/test/fixtures/source-map/output/source_map_enabled_by_api_node_modules.snapshot index f46c21dbe42057..decc8ee3632f70 100644 --- a/test/fixtures/source-map/output/source_map_enabled_by_api_node_modules.snapshot +++ b/test/fixtures/source-map/output/source_map_enabled_by_api_node_modules.snapshot @@ -1,12 +1,12 @@ Error: an error! - at functionD (*node_modules*error-stack*enclosing-call-site.js:16:17) - at functionC (*node_modules*error-stack*enclosing-call-site.js:10:3) - at functionB (*node_modules*error-stack*enclosing-call-site.js:6:3) - at functionA (*node_modules*error-stack*enclosing-call-site.js:2:3) - at Object. (*node_modules*error-stack*enclosing-call-site.js:24:3) + at functionD (*/test/fixtures/source-map/node_modules/error-stack/enclosing-call-site.js:16:17) + at functionC (*/test/fixtures/source-map/node_modules/error-stack/enclosing-call-site.js:10:3) + at functionB (*/test/fixtures/source-map/node_modules/error-stack/enclosing-call-site.js:6:3) + at functionA (*/test/fixtures/source-map/node_modules/error-stack/enclosing-call-site.js:2:3) + at Object. (*/test/fixtures/source-map/node_modules/error-stack/enclosing-call-site.js:24:3) Error: an error! - at functionD (*node_modules*error-stack*enclosing-call-site-min.js:1:156) - at functionC (*node_modules*error-stack*enclosing-call-site-min.js:1:97) - at functionB (*node_modules*error-stack*enclosing-call-site-min.js:1:60) - at functionA (*node_modules*error-stack*enclosing-call-site-min.js:1:26) - at Object. (*node_modules*error-stack*enclosing-call-site-min.js:1:199) + at functionD (*/test/fixtures/source-map/node_modules/error-stack/enclosing-call-site-min.js:1:156) + at functionC (*/test/fixtures/source-map/node_modules/error-stack/enclosing-call-site-min.js:1:97) + at functionB (*/test/fixtures/source-map/node_modules/error-stack/enclosing-call-site-min.js:1:60) + at functionA (*/test/fixtures/source-map/node_modules/error-stack/enclosing-call-site-min.js:1:26) + at Object. (*/test/fixtures/source-map/node_modules/error-stack/enclosing-call-site-min.js:1:199) diff --git a/test/fixtures/source-map/output/source_map_enabled_by_process_api.snapshot b/test/fixtures/source-map/output/source_map_enabled_by_process_api.snapshot index 082b3f310ed4f9..9fe43a29865b35 100644 --- a/test/fixtures/source-map/output/source_map_enabled_by_process_api.snapshot +++ b/test/fixtures/source-map/output/source_map_enabled_by_process_api.snapshot @@ -1,12 +1,12 @@ Error: an error! - at functionD (*enclosing-call-site.js:16:17) - at functionC (*enclosing-call-site.js:10:3) - at functionB (*enclosing-call-site.js:6:3) - at functionA (*enclosing-call-site.js:2:3) - at Object. (*enclosing-call-site.js:24:3) + at functionD (*/test/fixtures/source-map/enclosing-call-site.js:16:17) + at functionC (*/test/fixtures/source-map/enclosing-call-site.js:10:3) + at functionB (*/test/fixtures/source-map/enclosing-call-site.js:6:3) + at functionA (*/test/fixtures/source-map/enclosing-call-site.js:2:3) + at Object. (*/test/fixtures/source-map/enclosing-call-site.js:24:3) Error: an error! - at functionD (*enclosing-call-site-min.js:1:156) - at functionC (*enclosing-call-site-min.js:1:97) - at functionB (*enclosing-call-site-min.js:1:60) - at functionA (*enclosing-call-site-min.js:1:26) - at Object. (*enclosing-call-site-min.js:1:199) + at functionD (*/test/fixtures/source-map/enclosing-call-site-min.js:1:156) + at functionC (*/test/fixtures/source-map/enclosing-call-site-min.js:1:97) + at functionB (*/test/fixtures/source-map/enclosing-call-site-min.js:1:60) + at functionA (*/test/fixtures/source-map/enclosing-call-site-min.js:1:26) + at Object. (*/test/fixtures/source-map/enclosing-call-site-min.js:1:199) diff --git a/test/fixtures/source-map/output/source_map_enclosing_function.snapshot b/test/fixtures/source-map/output/source_map_enclosing_function.snapshot index 976cd4fdbbc6e9..b60f988be3214e 100644 --- a/test/fixtures/source-map/output/source_map_enclosing_function.snapshot +++ b/test/fixtures/source-map/output/source_map_enclosing_function.snapshot @@ -1,13 +1,13 @@ -*enclosing-call-site.js:26 +*/test/fixtures/source-map/enclosing-call-site.js:26 throw err ^ Error: an error! - at functionD (*enclosing-call-site.js:16:17) - at functionC (*enclosing-call-site.js:10:3) - at functionB (*enclosing-call-site.js:6:3) - at functionA (*enclosing-call-site.js:2:3) - at Object. (*enclosing-call-site.js:24:3) + at functionD (*/test/fixtures/source-map/enclosing-call-site.js:16:17) + at functionC (*/test/fixtures/source-map/enclosing-call-site.js:10:3) + at functionB (*/test/fixtures/source-map/enclosing-call-site.js:6:3) + at functionA (*/test/fixtures/source-map/enclosing-call-site.js:2:3) + at Object. (*/test/fixtures/source-map/enclosing-call-site.js:24:3) Node.js * diff --git a/test/fixtures/source-map/output/source_map_eval.js b/test/fixtures/source-map/output/source_map_eval.js index 5492c179248180..6454096f58a5a6 100644 --- a/test/fixtures/source-map/output/source_map_eval.js +++ b/test/fixtures/source-map/output/source_map_eval.js @@ -6,5 +6,6 @@ Error.stackTraceLimit = 3; const fs = require('fs'); -const content = fs.readFileSync(require.resolve('../tabs.js'), 'utf8'); +const content = fs.readFileSync(require.resolve('../tabs-source-url.js'), 'utf8'); +// SourceURL magic comment is hardcoded in the source content. eval(content); diff --git a/test/fixtures/source-map/output/source_map_eval.snapshot b/test/fixtures/source-map/output/source_map_eval.snapshot index a4fcdb25282dfa..ff636e44063aa7 100644 --- a/test/fixtures/source-map/output/source_map_eval.snapshot +++ b/test/fixtures/source-map/output/source_map_eval.snapshot @@ -1,11 +1,11 @@ -*tabs.coffee:26 +*/synthesized/workspace/tabs-source-url.coffee:26 alert "I knew it!" ^ ReferenceError: alert is not defined - at Object.eval (*tabs.coffee:26:2) - at eval (*tabs.coffee:1:14) - at Object. (*output*source_map_eval.js:10:1) + at Object.eval (*/synthesized/workspace/tabs-source-url.coffee:26:2) + at eval (*/synthesized/workspace/tabs-source-url.coffee:1:14) + at Object. (*/test/fixtures/source-map/output/source_map_eval.js:11:1) Node.js * diff --git a/test/fixtures/source-map/output/source_map_no_source_file.snapshot b/test/fixtures/source-map/output/source_map_no_source_file.snapshot index cf9329747ba692..6d0e0cf6db727e 100644 --- a/test/fixtures/source-map/output/source_map_no_source_file.snapshot +++ b/test/fixtures/source-map/output/source_map_no_source_file.snapshot @@ -1,9 +1,9 @@ -*no-source.js:2 +*/test/fixtures/source-map/no-source.js:2 throw new Error('foo'); ^ Error: foo - at Throw (*file-not-exists.ts:2:9) - at Object. (*file-not-exists.ts:5:1) + at Throw (*/test/fixtures/source-map/file-not-exists.ts:2:9) + at Object. (*/test/fixtures/source-map/file-not-exists.ts:5:1) Node.js * diff --git a/test/fixtures/source-map/output/source_map_prepare_stack_trace.snapshot b/test/fixtures/source-map/output/source_map_prepare_stack_trace.snapshot index 9e9740a26fc34e..57bac36a2e5214 100644 --- a/test/fixtures/source-map/output/source_map_prepare_stack_trace.snapshot +++ b/test/fixtures/source-map/output/source_map_prepare_stack_trace.snapshot @@ -1,10 +1,10 @@ Error: an error! - at functionD (*enclosing-call-site.js:16:17) - at functionB (*enclosing-call-site.js:6:3) - at functionA (*enclosing-call-site.js:2:3) - at Object. (*enclosing-call-site.js:24:3) + at functionD (*/test/fixtures/source-map/enclosing-call-site.js:16:17) + at functionB (*/test/fixtures/source-map/enclosing-call-site.js:6:3) + at functionA (*/test/fixtures/source-map/enclosing-call-site.js:2:3) + at Object. (*/test/fixtures/source-map/enclosing-call-site.js:24:3) Error: an error! - at functionD (*enclosing-call-site-min.js:1:156) - at functionB (*enclosing-call-site-min.js:1:60) - at functionA (*enclosing-call-site-min.js:1:26) - at Object. (*enclosing-call-site-min.js:1:199) + at functionD (*/test/fixtures/source-map/enclosing-call-site-min.js:1:156) + at functionB (*/test/fixtures/source-map/enclosing-call-site-min.js:1:60) + at functionA (*/test/fixtures/source-map/enclosing-call-site-min.js:1:26) + at Object. (*/test/fixtures/source-map/enclosing-call-site-min.js:1:199) diff --git a/test/fixtures/source-map/output/source_map_reference_error_tabs.snapshot b/test/fixtures/source-map/output/source_map_reference_error_tabs.snapshot index 97d02f176c0cb7..2043bd0a88e897 100644 --- a/test/fixtures/source-map/output/source_map_reference_error_tabs.snapshot +++ b/test/fixtures/source-map/output/source_map_reference_error_tabs.snapshot @@ -1,10 +1,10 @@ -*tabs.coffee:26 +*/test/fixtures/source-map/tabs.coffee:26 alert "I knew it!" ^ ReferenceError: alert is not defined - at Object. (*tabs.coffee:26:2) - at Object. (*tabs.coffee:1:14) + at Object. (*/test/fixtures/source-map/tabs.coffee:26:2) + at Object. (*/test/fixtures/source-map/tabs.coffee:1:14) Node.js * diff --git a/test/fixtures/source-map/output/source_map_sourcemapping_url_string.snapshot b/test/fixtures/source-map/output/source_map_sourcemapping_url_string.snapshot index 6a109c904e1155..52d4e6a28ee5ce 100644 --- a/test/fixtures/source-map/output/source_map_sourcemapping_url_string.snapshot +++ b/test/fixtures/source-map/output/source_map_sourcemapping_url_string.snapshot @@ -1,3 +1,3 @@ Error: an exception. - at Object. (*typescript-sourcemapping_url_string.ts:3:7) + at Object. (*/test/fixtures/source-map/typescript-sourcemapping_url_string.ts:3:7) * diff --git a/test/fixtures/source-map/output/source_map_throw_async_stack_trace.snapshot b/test/fixtures/source-map/output/source_map_throw_async_stack_trace.snapshot index 8f7f0490587585..f53aec68ce8bb3 100644 --- a/test/fixtures/source-map/output/source_map_throw_async_stack_trace.snapshot +++ b/test/fixtures/source-map/output/source_map_throw_async_stack_trace.snapshot @@ -1,11 +1,11 @@ -*output*source_map_throw_async_stack_trace.mts:13 +*/test/fixtures/source-map/output/source_map_throw_async_stack_trace.mts:13 throw new Error('message') ^ Error: message - at Throw (*output*source_map_throw_async_stack_trace.mts:13:9) + at Throw (*/test/fixtures/source-map/output/source_map_throw_async_stack_trace.mts:13:9) at async Promise.all (index 3) - at async main (*output*source_map_throw_async_stack_trace.mts:17:3) + at async main (*/test/fixtures/source-map/output/source_map_throw_async_stack_trace.mts:17:3) Node.js * diff --git a/test/fixtures/source-map/output/source_map_throw_catch.snapshot b/test/fixtures/source-map/output/source_map_throw_catch.snapshot index 5eaffbfbf7874f..6e80e500c8cf17 100644 --- a/test/fixtures/source-map/output/source_map_throw_catch.snapshot +++ b/test/fixtures/source-map/output/source_map_throw_catch.snapshot @@ -1,4 +1,4 @@ reachable Error: an exception - at branch (*typescript-throw.ts:18:11) - at Object. (*typescript-throw.ts:24:1) + at branch (*/test/fixtures/source-map/typescript-throw.ts:18:11) + at Object. (*/test/fixtures/source-map/typescript-throw.ts:24:1) diff --git a/test/fixtures/source-map/output/source_map_throw_construct.snapshot b/test/fixtures/source-map/output/source_map_throw_construct.snapshot index 8618d4b51a46ec..dc28053240aa0b 100644 --- a/test/fixtures/source-map/output/source_map_throw_construct.snapshot +++ b/test/fixtures/source-map/output/source_map_throw_construct.snapshot @@ -1,11 +1,11 @@ -*output*source_map_throw_construct.mts:13 +*/test/fixtures/source-map/output/source_map_throw_construct.mts:13 throw new Error('message'); ^ Error: message - at new Foo (*output*source_map_throw_construct.mts:13:11) - at (*output*source_map_throw_construct.mts:17:1) + at new Foo (*/test/fixtures/source-map/output/source_map_throw_construct.mts:13:11) + at (*/test/fixtures/source-map/output/source_map_throw_construct.mts:17:1) * * * diff --git a/test/fixtures/source-map/output/source_map_throw_first_tick.snapshot b/test/fixtures/source-map/output/source_map_throw_first_tick.snapshot index fba8c95cc1103f..e129d73ef1581c 100644 --- a/test/fixtures/source-map/output/source_map_throw_first_tick.snapshot +++ b/test/fixtures/source-map/output/source_map_throw_first_tick.snapshot @@ -1,11 +1,11 @@ reachable -*typescript-throw.ts:18 +*/test/fixtures/source-map/typescript-throw.ts:18 throw Error('an exception'); ^ Error: an exception - at branch (*typescript-throw.ts:18:11) - at Object. (*typescript-throw.ts:24:1) + at branch (*/test/fixtures/source-map/typescript-throw.ts:18:11) + at Object. (*/test/fixtures/source-map/typescript-throw.ts:24:1) Node.js * diff --git a/test/fixtures/source-map/output/source_map_throw_icu.snapshot b/test/fixtures/source-map/output/source_map_throw_icu.snapshot index 425495062e6423..4b2853479b9576 100644 --- a/test/fixtures/source-map/output/source_map_throw_icu.snapshot +++ b/test/fixtures/source-map/output/source_map_throw_icu.snapshot @@ -1,10 +1,10 @@ -*icu.jsx:3 +*/test/fixtures/source-map/icu.jsx:3 ("あ 🐕 🐕", throw Error("an error")); ^ Error: an error - at Object.createElement (*icu.jsx:3:23) - at Object. (*icu.jsx:9:5) + at Object.createElement (*/test/fixtures/source-map/icu.jsx:3:23) + at Object. (*/test/fixtures/source-map/icu.jsx:9:5) Node.js * diff --git a/test/fixtures/source-map/output/source_map_throw_set_immediate.snapshot b/test/fixtures/source-map/output/source_map_throw_set_immediate.snapshot index ec9f1346ca5e0c..4cf4d52a16ea93 100644 --- a/test/fixtures/source-map/output/source_map_throw_set_immediate.snapshot +++ b/test/fixtures/source-map/output/source_map_throw_set_immediate.snapshot @@ -1,11 +1,11 @@ -*uglify-throw-original.js:5 +*/test/fixtures/source-map/uglify-throw-original.js:5 throw Error('goodbye'); ^ Error: goodbye - at Hello (*uglify-throw-original.js:5:9) - at Immediate. (*uglify-throw-original.js:9:3) + at Hello (*/test/fixtures/source-map/uglify-throw-original.js:5:9) + at Immediate. (*/test/fixtures/source-map/uglify-throw-original.js:9:3) * Node.js * diff --git a/test/fixtures/source-map/tabs-source-url.coffee b/test/fixtures/source-map/tabs-source-url.coffee new file mode 100644 index 00000000000000..7e73a605d32fc9 --- /dev/null +++ b/test/fixtures/source-map/tabs-source-url.coffee @@ -0,0 +1,34 @@ +# Assignment: +number = 42 +opposite = true + +# Conditions: +number = -42 if opposite + +# Functions: +square = (x) -> x * x + +# Arrays: +list = [1, 2, 3, 4, 5] + +# Objects: +math = + root: Math.sqrt + square: square + cube: (x) -> x * square x + +# Splats: +race = (winner, runners...) -> + print winner, runners + +# Existence: +if true + alert "I knew it!" + +# Array comprehensions: +cubes = (math.cube num for num in list) + +# To reproduce: +# cd test/fixtures/source-map +# npx --package=coffeescript@2.5.1 -- coffee -M --compile tabs-source-url.coffee +# sed -i -e "s/$(pwd | sed -e "s/\//\\\\\//g")/.\\/synthesized\\/workspace/g" tabs-source-url.js diff --git a/test/fixtures/source-map/tabs-source-url.js b/test/fixtures/source-map/tabs-source-url.js new file mode 100644 index 00000000000000..a4b43806a5f2f5 --- /dev/null +++ b/test/fixtures/source-map/tabs-source-url.js @@ -0,0 +1,61 @@ +// Generated by CoffeeScript 2.5.1 +(function() { + // Assignment: + var cubes, list, math, num, number, opposite, race, square; + + number = 42; + + opposite = true; + + if (opposite) { + // Conditions: + number = -42; + } + + // Functions: + square = function(x) { + return x * x; + }; + + // Arrays: + list = [1, 2, 3, 4, 5]; + + // Objects: + math = { + root: Math.sqrt, + square: square, + cube: function(x) { + return x * square(x); + } + }; + + // Splats: + race = function(winner, ...runners) { + return print(winner, runners); + }; + + // Existence: + if (true) { + alert("I knew it!"); + } + + // Array comprehensions: + cubes = (function() { + var i, len, results; + results = []; + for (i = 0, len = list.length; i < len; i++) { + num = list[i]; + results.push(math.cube(num)); + } + return results; + })(); + + // To reproduce: +// cd test/fixtures/source-map +// npx --package=coffeescript@2.5.1 -- coffee -M --compile tabs-source-url.coffee +// sed -i -e "s/$(pwd | sed -e "s/\//\\\\\//g")/.\\/synthesized\\/workspace/g" tabs-source-url.js + +}).call(this); + +//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidGFicy1zb3VyY2UtdXJsLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsidGFicy1zb3VyY2UtdXJsLmNvZmZlZSJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiO0FBQWE7RUFBQTtBQUFBLE1BQUEsS0FBQSxFQUFBLElBQUEsRUFBQSxJQUFBLEVBQUEsR0FBQSxFQUFBLE1BQUEsRUFBQSxRQUFBLEVBQUEsSUFBQSxFQUFBOztFQUNiLE1BQUEsR0FBVzs7RUFDWCxRQUFBLEdBQVc7O0VBR1gsSUFBZ0IsUUFBaEI7O0lBQUEsTUFBQSxHQUFTLENBQUMsR0FBVjtHQUxhOzs7RUFRYixNQUFBLEdBQVMsUUFBQSxDQUFDLENBQUQsQ0FBQTtXQUFPLENBQUEsR0FBSTtFQUFYLEVBUkk7OztFQVdiLElBQUEsR0FBTyxDQUFDLENBQUQsRUFBSSxDQUFKLEVBQU8sQ0FBUCxFQUFVLENBQVYsRUFBYSxDQUFiLEVBWE07OztFQWNiLElBQUEsR0FDQztJQUFBLElBQUEsRUFBUSxJQUFJLENBQUMsSUFBYjtJQUNBLE1BQUEsRUFBUSxNQURSO0lBRUEsSUFBQSxFQUFRLFFBQUEsQ0FBQyxDQUFELENBQUE7YUFBTyxDQUFBLEdBQUksTUFBQSxDQUFPLENBQVA7SUFBWDtFQUZSLEVBZlk7OztFQW9CYixJQUFBLEdBQU8sUUFBQSxDQUFDLE1BQUQsRUFBQSxHQUFTLE9BQVQsQ0FBQTtXQUNOLEtBQUEsQ0FBTSxNQUFOLEVBQWMsT0FBZDtFQURNLEVBcEJNOzs7RUF3QmIsSUFBRyxJQUFIO0lBQ0MsS0FBQSxDQUFNLFlBQU4sRUFERDtHQXhCYTs7O0VBNEJiLEtBQUE7O0FBQVM7SUFBQSxLQUFBLHNDQUFBOzttQkFBQSxJQUFJLENBQUMsSUFBTCxDQUFVLEdBQVY7SUFBQSxDQUFBOzs7O0VBNUJJOzs7O0FBQUEiLCJzb3VyY2VzQ29udGVudCI6WyIjIEFzc2lnbm1lbnQ6XG5udW1iZXIgICA9IDQyXG5vcHBvc2l0ZSA9IHRydWVcblxuIyBDb25kaXRpb25zOlxubnVtYmVyID0gLTQyIGlmIG9wcG9zaXRlXG5cbiMgRnVuY3Rpb25zOlxuc3F1YXJlID0gKHgpIC0+IHggKiB4XG5cbiMgQXJyYXlzOlxubGlzdCA9IFsxLCAyLCAzLCA0LCA1XVxuXG4jIE9iamVjdHM6XG5tYXRoID1cblx0cm9vdDogICBNYXRoLnNxcnRcblx0c3F1YXJlOiBzcXVhcmVcblx0Y3ViZTogICAoeCkgLT4geCAqIHNxdWFyZSB4XG5cbiMgU3BsYXRzOlxucmFjZSA9ICh3aW5uZXIsIHJ1bm5lcnMuLi4pIC0+XG5cdHByaW50IHdpbm5lciwgcnVubmVyc1xuXG4jIEV4aXN0ZW5jZTpcbmlmIHRydWVcblx0YWxlcnQgXCJJIGtuZXcgaXQhXCJcblxuIyBBcnJheSBjb21wcmVoZW5zaW9uczpcbmN1YmVzID0gKG1hdGguY3ViZSBudW0gZm9yIG51bSBpbiBsaXN0KVxuXG4jIFRvIHJlcHJvZHVjZTpcbiMgY2QgdGVzdC9maXh0dXJlcy9zb3VyY2UtbWFwXG4jIG5weCAtLXBhY2thZ2U9Y29mZmVlc2NyaXB0QDIuNS4xIC0tIGNvZmZlZSAtTSAtLWNvbXBpbGUgdGFicy1zb3VyY2UtdXJsLmNvZmZlZVxuIyBzZWQgLWkgLWUgXCJzLyQocHdkIHwgc2VkIC1lIFwicy9cXC8vXFxcXFxcXFxcXC8vZ1wiKS8uXFxcXC9zeW50aGVzaXplZFxcXFwvd29ya3NwYWNlL2dcIiB0YWJzLXNvdXJjZS11cmwuanNcbiJdfQ== +//# sourceURL=./synthesized/workspace/tabs-source-url.coffee \ No newline at end of file diff --git a/test/parallel/test-node-output-sourcemaps.mjs b/test/parallel/test-node-output-sourcemaps.mjs index 29cc5eb711f176..81c36934ba0f3e 100644 --- a/test/parallel/test-node-output-sourcemaps.mjs +++ b/test/parallel/test-node-output-sourcemaps.mjs @@ -1,29 +1,17 @@ -import * as common from '../common/index.mjs'; +import '../common/index.mjs'; import * as fixtures from '../common/fixtures.mjs'; import * as snapshot from '../common/assertSnapshot.js'; -import * as path from 'node:path'; import { describe, it } from 'node:test'; describe('sourcemaps output', { concurrency: !process.env.TEST_PARALLEL }, () => { - function normalize(str) { - const result = str - .replaceAll(snapshot.replaceWindowsPaths(process.cwd()), '') - .replaceAll('//', '*') - .replaceAll('/Users/bencoe/oss/coffee-script-test', '') - .replaceAll(/\/(\w)/g, '*$1') - .replaceAll('*test*', '*') - .replaceAll('*fixtures*source-map*', '*') - .replaceAll(/(\W+).*node:.*/g, '$1*'); - if (common.isWindows) { - const currentDeviceLetter = path.parse(process.cwd()).root.substring(0, 1).toLowerCase(); - const regex = new RegExp(`${currentDeviceLetter}:/?`, 'gi'); - return result.replaceAll(regex, ''); - } - return result; - } const defaultTransform = snapshot - .transform(snapshot.replaceWindowsLineEndings, snapshot.replaceWindowsPaths, - normalize, snapshot.replaceNodeVersion); + .transform( + snapshot.replaceWindowsLineEndings, + snapshot.transformProjectRoot('*'), + snapshot.replaceWindowsPaths, + snapshot.replaceInternalStackTrace, + snapshot.replaceNodeVersion + ); const tests = [ { name: 'source-map/output/source_map_disabled_by_api.js' }, diff --git a/test/parallel/test-runner-output.mjs b/test/parallel/test-runner-output.mjs index 081d4673ed313d..a9ee2e36eef3fc 100644 --- a/test/parallel/test-runner-output.mjs +++ b/test/parallel/test-runner-output.mjs @@ -70,7 +70,7 @@ const defaultTransform = snapshot.transform( snapshot.replaceWindowsLineEndings, snapshot.replaceStackTrace, removeWindowsPathEscaping, - snapshot.replaceFullPaths, + snapshot.transformProjectRoot(), snapshot.replaceWindowsPaths, replaceTestDuration, replaceTestLocationLine, @@ -90,7 +90,7 @@ const junitTransform = snapshot.transform( const lcovTransform = snapshot.transform( snapshot.replaceWindowsLineEndings, snapshot.replaceStackTrace, - snapshot.replaceFullPaths, + snapshot.transformProjectRoot(), snapshot.replaceWindowsPaths, pickTestFileFromLcov );