Skip to content

Commit 54f1b7e

Browse files
authored
Merge pull request #1099 from Agoric/mfig/source-map-lineno
Make output from bundleSource correspond to source map lines
2 parents 7f6e271 + 6c7d3bb commit 54f1b7e

File tree

11 files changed

+82
-28
lines changed

11 files changed

+82
-28
lines changed

packages/agoric-cli/integration-tests/test-workflow.js

+19-4
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,32 @@ test('workflow', async t => {
3636
// Kill an entire process group.
3737
const pkill = (cp, signal = 'SIGINT') => process.kill(-cp.pid, signal);
3838

39+
function pspawnStdout(...args) {
40+
let output = '';
41+
const ps = pspawn(...args);
42+
ps.cp.stdout.on('data', chunk => {
43+
output += chunk.toString('utf-8');
44+
});
45+
ps.then(ret => {
46+
if (ret !== 0) {
47+
process.stdout.write(output);
48+
}
49+
});
50+
return ps;
51+
}
52+
3953
// Run all main programs with the '--sdk' flag if we are in agoric-sdk.
4054
const extraArgs = fs.existsSync(`${__dirname}/../../cosmic-swingset`)
4155
? ['--sdk']
4256
: [];
43-
const myMain = args => {
57+
function myMain(args) {
4458
// console.error('running agoric-cli', ...extraArgs, ...args);
45-
return pspawn(`agoric`, [...extraArgs, ...args], {
59+
return pspawnStdout(`agoric`, [...extraArgs, ...args], {
4660
stdio: ['ignore', 'pipe', 'inherit'],
61+
env: { ...process.env, DEBUG: 'agoric' },
4762
detached: true,
4863
});
49-
};
64+
}
5065

5166
const olddir = process.cwd();
5267
const { name, removeCallback } = tmp.dirSync({
@@ -150,7 +165,7 @@ test('workflow', async t => {
150165

151166
// ==============
152167
// cd ui && yarn install
153-
const instRet = await pspawn(`yarn`, ['install'], {
168+
const instRet = await pspawnStdout(`yarn`, ['install'], {
154169
stdio: ['ignore', 'pipe', 'inherit'],
155170
cwd: 'ui',
156171
detached: true,

packages/bundle-source/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
"@rollup/plugin-node-resolve": "^7.1.1",
2525
"acorn": "^7.1.0",
2626
"esm": "^3.2.5",
27-
"rollup": "^1.32.0"
27+
"rollup": "^1.32.0",
28+
"source-map": "^0.7.3"
2829
},
2930
"keywords": [],
3031
"files": [

packages/bundle-source/src/index.js

+40-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import commonjs0 from '@rollup/plugin-commonjs';
55
import eventualSend from '@agoric/acorn-eventual-send';
66
import * as acorn from 'acorn';
77

8+
import { SourceMapConsumer } from 'source-map';
9+
810
const DEFAULT_MODULE_FORMAT = 'nestedEvaluate';
911
const DEFAULT_FILE_PREFIX = '/bundled-source';
1012
const SUPPORTED_FORMATS = ['getExport', 'nestedEvaluate'];
@@ -55,11 +57,46 @@ export default async function bundleSource(
5557
if (isEntry) {
5658
entrypoint = fileName;
5759
}
60+
let unmappedCode = code;
61+
if (
62+
moduleFormat === 'nestedEvaluate' &&
63+
!fileName.startsWith('_virtual/')
64+
) {
65+
unmappedCode = '';
66+
// eslint-disable-next-line no-await-in-loop
67+
const consumer = await new SourceMapConsumer(chunk.map);
68+
const genLines = code.split('\n');
69+
let lastLine = 1;
70+
for (let genLine = 0; genLine < genLines.length; genLine += 1) {
71+
const pos = consumer.originalPositionFor({
72+
line: genLine + 1,
73+
column: 0,
74+
});
75+
const { line: origLine } = pos;
76+
77+
const srcLine = origLine === null ? lastLine : origLine;
78+
const priorChar = unmappedCode[unmappedCode.length - 1];
79+
if (
80+
srcLine === lastLine &&
81+
!['\n', ';', '{', undefined].includes(priorChar) &&
82+
!['}'].includes(genLines[genLine][0])
83+
) {
84+
unmappedCode += `;`;
85+
}
86+
while (lastLine < srcLine) {
87+
unmappedCode += `\n`;
88+
lastLine += 1;
89+
}
90+
unmappedCode += genLines[genLine];
91+
}
92+
}
93+
5894
// Rewrite apparent import expressions so that they don't fail under SES.
5995
// We also do apparent HTML comments.
60-
const defangedCode = code
96+
const defangedCode = unmappedCode
6197
.replace(IMPORT_RE, '$1notreally')
6298
.replace(HTML_COMMENT_RE, '<->');
99+
// console.log(`<<<<<< ${fileName}\n${defangedCode}\n>>>>>>>>`);
63100
sourceBundle[fileName] = defangedCode;
64101
}
65102

@@ -191,7 +228,7 @@ ${sourceMap}`;
191228
);
192229
}
193230

194-
// log('evaluating', typeof nestedEvaluate, code);
231+
// log('evaluating', code);
195232
return nestedEvaluate(code)(contextRequire);
196233
}
197234

@@ -212,7 +249,7 @@ function getExportWithNestedEvaluate(filePrefix) {
212249
${computeExports}
213250
214251
// Evaluate the entrypoint recursively.
215-
return computeExports(entrypoint, { require, log(...args) { return console.log(...args); } });
252+
return computeExports(entrypoint, { require });
216253
}
217254
${sourceMap}`;
218255
}

packages/bundle-source/test/sanity.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,10 @@ test('nestedEvaluate', async t => {
2727

2828
const bundle = ex1.default();
2929
const err = bundle.makeError('foo');
30+
// console.log(err.stack);
3031
t.assert(
31-
err.stack.indexOf('(/bundled-source/encourage.js:') >= 0,
32-
'bundled source is in stack trace',
32+
err.stack.indexOf('(/bundled-source/encourage.js:3:') >= 0,
33+
'bundled source is in stack trace with correct line number',
3334
);
3435

3536
const {
@@ -94,7 +95,7 @@ test('getExport', async t => {
9495
const srcMap2 = `(${src2})\n${map2}`;
9596

9697
const nestedEvaluate = src => {
97-
// console.log('========== evaluating', src);
98+
// console.log('========== evaluating', src, '\n=========');
9899
return evaluate(src, { require, nestedEvaluate });
99100
};
100101
// eslint-disable-next-line no-eval

packages/default-evaluate-options/package.json

+3-4
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@
22
"name": "@agoric/default-evaluate-options",
33
"version": "0.3.5",
44
"description": "Agoric default evaluation options for Realms and SES",
5-
"main": "dist/default-evaluate-options.cjs.js",
6-
"module": "src/index.js",
7-
"browser": "dist/default-evaluate-options.umd.js",
5+
"main": "src/index.js",
86
"scripts": {
9-
"build": "rollup -c",
7+
"build": "exit 0",
8+
"build:rollup": "rollup -c",
109
"test": "tape -r esm 'test/**/*.js'",
1110
"lint-fix": "eslint --fix '**/*.js'",
1211
"lint-check": "eslint '**/*.js'",

packages/evaluate/src/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ export const makeEvaluators = (makerOptions = {}) => {
100100
}
101101
})`;
102102

103+
// console.log('##### evaluate ######\n${src}\n###### end evaluate #####');
104+
103105
// The eval below is indirect, so that we are only in the global scope.
104106
// eslint-disable-next-line no-eval
105107
return (1, eval)(scopedEval)(sourceState.endowments)(src);

packages/eventual-send/package.json

+2-4
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,11 @@
22
"name": "@agoric/eventual-send",
33
"version": "0.9.1",
44
"description": "Extend a Promise class to implement the eventual-send API",
5-
"main": "dist/eventual-send.cjs.js",
6-
"module": "src/index.js",
7-
"browser": "dist/eventual-send.umd.js",
5+
"main": "src/index.js",
86
"types": "src/index.d.ts",
97
"scripts": {
108
"test": "tape -r esm 'test/**/test*.js'",
11-
"build": "rollup -c",
9+
"build": "exit 0",
1210
"lint-fix": "eslint --fix '**/*.js'",
1311
"lint-check": "eslint '**/*.js'"
1412
},

packages/transform-eventual-send/package.json

+2-4
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,12 @@
22
"name": "@agoric/transform-eventual-send",
33
"version": "1.2.4",
44
"description": "transform-eventual-send",
5-
"main": "dist/transform-eventual-send.cjs.js",
6-
"module": "src/index.js",
7-
"browser": "dist/transform-eventual-send.umd.js",
5+
"main": "src/index.js",
86
"scripts": {
97
"test": "tape -r esm 'test/**/*.js'",
108
"lint-fix": "eslint --fix '**/*.js'",
119
"lint-check": "eslint '**/*.js'",
12-
"build": "node -r esm ./scripts/build-esend.js && rollup -c rollup.config.js"
10+
"build": "node -r esm ./scripts/build-esend.js"
1311
},
1412
"devDependencies": {
1513
"@agoric/acorn-eventual-send": "^2.0.4",

packages/transform-eventual-send/src/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ function makeEventualSendTransformer(parser, generate) {
6464
plugins: ['eventualSend'],
6565
});
6666
// Create the source from the ast.
67-
const output = generate(ast, {}, source);
67+
const output = generate(ast, { retainLines: true }, source);
6868

6969
// Work around Babel appending semicolons.
7070
const maybeSource = output.code;

packages/transform-metering/package.json

+2-4
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,12 @@
22
"name": "@agoric/transform-metering",
33
"version": "1.2.4",
44
"description": "transform-metering",
5-
"main": "dist/transform-metering.cjs.js",
6-
"module": "src/index.js",
7-
"browser": "dist/transform-metering.umd.js",
5+
"main": "src/index.js",
86
"scripts": {
97
"test": "tape -r esm 'test/**/*.js'",
108
"lint-fix": "eslint --fix '**/*.{js,jsx}'",
119
"lint-check": "eslint '**/*.{js,jsx}'",
12-
"build": "rollup -c rollup.config.js"
10+
"build": "exit 0"
1311
},
1412
"devDependencies": {
1513
"@babel/core": "^7.5.0",

yarn.lock

+5
Original file line numberDiff line numberDiff line change
@@ -13460,6 +13460,11 @@ source-map@^0.5.0, source-map@^0.5.6:
1346013460
resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.5.7.tgz#8a039d2d1021d22d1ea14c80d8ea468ba2ef3fcc"
1346113461
integrity sha1-igOdLRAh0i0eoUyA2OpGi6LvP8w=
1346213462

13463+
source-map@^0.7.3:
13464+
version "0.7.3"
13465+
resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.7.3.tgz#5302f8169031735226544092e64981f751750383"
13466+
integrity sha512-CkCj6giN3S+n9qrYiBTX5gystlENnRW5jZeNLHpe6aue+SrHcG5VYwujhW9s4dY31mEGsxBDrHR6oI69fTXsaQ==
13467+
1346313468
sourcemap-codec@^1.4.4:
1346413469
version "1.4.6"
1346513470
resolved "https://registry.yarnpkg.com/sourcemap-codec/-/sourcemap-codec-1.4.6.tgz#e30a74f0402bad09807640d39e971090a08ce1e9"

0 commit comments

Comments
 (0)