Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/cashc/src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ function compileCode(
requires: optimisationResult.requires,
sourceTags: generateSourceTags(optimisationResult.sourceTags) || undefined,
functions: traversal.frames.length > 0 ? traversal.frames : undefined,
sources: Object.keys(traversal.sources).length > 0 ? traversal.sources : undefined,
inlineRanges: generateInlineRanges(optimisationResult.inlineRanges) || undefined,
};

Expand Down
51 changes: 42 additions & 9 deletions packages/cashc/src/dependency-resolution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ export interface ImportResolver {
read(canonicalPath: string): string | undefined;
dirname(canonicalPath: string): string;
sourceName(canonicalPath: string): string;
// A name no other file resolves to, used when two files share a `sourceName`
uniqueSourceName(canonicalPath: string): string;
}

export function createDiskResolver(rootDir: string): ImportResolver {
Expand All @@ -41,6 +43,9 @@ export function createDiskResolver(rootDir: string): ImportResolver {
const relativePath = getPackageImportPath(canonicalPath) ?? path.relative(rootDir, canonicalPath);
return relativePath.split(path.sep).join(path.posix.sep);
},
// The path relative to the root is unique per file, unlike the package path, which a local file
// or another installed copy of the package can share
uniqueSourceName: (canonicalPath) => path.relative(rootDir, canonicalPath).split(path.sep).join(path.posix.sep),
};
}

Expand All @@ -59,6 +64,7 @@ export function createMemoryResolver(files: Record<string, string>): ImportResol
read: (canonicalPath) => normalisedFiles[canonicalPath],
dirname: (canonicalPath) => path.posix.dirname(canonicalPath),
sourceName: (canonicalPath) => canonicalPath,
uniqueSourceName: (canonicalPath) => canonicalPath,
};
}

Expand Down Expand Up @@ -99,6 +105,7 @@ function collectImports(
): ImportedDefinitions {
const visitedPaths = new Set<string>();
const activePaths = new Set<string>();
const importedFiles: ImportedFile[] = [];

const collect = (currentImports: ImportNode[], currentDir: string): ImportedDefinitions[] =>
currentImports.flatMap((importNode) => {
Expand Down Expand Up @@ -126,15 +133,7 @@ function collectImports(
const importedAst = parseCode(importedSource, errorListener);
checkVersionConstraints(importedAst.pragmas, resolver.sourceName(canonicalPath));

// Record source provenance so debug frames can attribute to the imported file
importedAst.functions.forEach((func) => {
func.sourceCode = importedSource;
func.sourceFile = resolver.sourceName(canonicalPath);
});
importedAst.constants.forEach((constant) => {
constant.sourceCode = importedSource;
constant.sourceFile = resolver.sourceName(canonicalPath);
});
importedFiles.push({ canonicalPath, source: importedSource, ast: importedAst });

activePaths.add(canonicalPath);
const transitiveDefinitions = collect(importedAst.imports, resolver.dirname(canonicalPath));
Expand All @@ -147,12 +146,46 @@ function collectImports(
});

const collected = collect(imports, resolver.rootDir);
recordSourceProvenance(importedFiles, resolver);
return {
functions: collected.flatMap((definitions) => definitions.functions),
constants: collected.flatMap((definitions) => definitions.constants),
};
}

interface ImportedFile {
canonicalPath: string;
source: string;
ast: SourceFileNode;
}

// Record source provenance so debug frames can attribute to the imported file. The artifact keeps each
// file's source once, keyed by this name, so two different files must never share one. Files start out
// with their short name, and any name held by more than one file is replaced by each file's unique name,
// repeated until no name is shared: a replacement can itself equal another file's short name when the
// contract lives inside node_modules. Unique names never collide, so this ends
function recordSourceProvenance(importedFiles: ImportedFile[], resolver: ImportResolver): void {
const names = new Map(importedFiles.map(({ canonicalPath }) => [canonicalPath, resolver.sourceName(canonicalPath)]));

const sharedNames = (): Set<string> => {
const counts = new Map<string, number>();
names.forEach((name) => counts.set(name, (counts.get(name) ?? 0) + 1));
return new Set([...counts].filter(([, count]) => count > 1).map(([name]) => name));
};

for (let shared = sharedNames(); shared.size > 0; shared = sharedNames()) {
names.forEach((name, canonicalPath) => {
if (shared.has(name)) names.set(canonicalPath, resolver.uniqueSourceName(canonicalPath));
});
}

importedFiles.forEach(({ canonicalPath, source, ast }) => {
[...ast.functions, ...ast.constants].forEach((definition) => {
definition.sourceCode = source;
definition.sourceFile = names.get(canonicalPath);
});
});
}

function isPackageImport(importPath: string): boolean {
return !importPath.startsWith('./') && !importPath.startsWith('../') && !importPath.startsWith('/');
Expand Down
7 changes: 6 additions & 1 deletion packages/cashc/src/generation/GenerateTargetTraversal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ export default class GenerateTargetTraversal extends AstTraversal {
requires: RequireStatement[] = [];
sourceTags: SourceTagEntry[] = [];
frames: DebugFrame[] = [];
// Full text of each imported file that a frame refers to by its `sourceFile`
sources: Record<string, string> = {};
finalStackUsage: Record<string, StackItem> = {};

private scopeDepth = 0;
Expand Down Expand Up @@ -276,6 +278,10 @@ export default class GenerateTargetTraversal extends AstTraversal {
optimised: OptimiseBytecodeResult,
functionId?: number,
): DebugFrame {
if (node.sourceFile !== undefined && node.sourceCode !== undefined) {
this.sources[node.sourceFile] = node.sourceCode;
}

return {
id: functionId,
name: node.name,
Expand All @@ -284,7 +290,6 @@ export default class GenerateTargetTraversal extends AstTraversal {
bytecode: binToHex(scriptToBytecode(optimised.script)),
sourceMap: generateSourceMap(optimised.locationData),
sourceTags: generateSourceTags(optimised.sourceTags) || undefined,
source: node.sourceCode,
sourceFile: node.sourceFile,
logs: optimised.logs,
requires: optimised.requires,
Expand Down
21 changes: 15 additions & 6 deletions packages/cashc/test/generation/fixtures/import-fixtures/diamond.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ import fs from 'fs';
import { URL } from 'url';
import { Fixture } from '../../fixture-utils.js';

const readImportFixture = (name: string): string => fs.readFileSync(
new URL(`../../../import-fixtures/${name}`, import.meta.url),
{ encoding: 'utf-8' },
);

export const fixtures: Fixture[] = [
{
artifact: {
Expand All @@ -23,7 +28,6 @@ export const fixtures: Fixture[] = [
inputs: [{ name: 'a', type: 'int' }],
bytecode: '8b',
sourceMap: '2:11:2:16:1',
source: fs.readFileSync(new URL('../../../import-fixtures/leaf.cash', import.meta.url), { encoding: 'utf-8' }),
sourceFile: 'leaf.cash',
logs: [],
requires: [],
Expand All @@ -33,7 +37,6 @@ export const fixtures: Fixture[] = [
inputs: [{ name: 'a', type: 'int' }],
bytecode: '8b5295',
sourceMap: '3:11:3:18:1;:21::22:0;:11:::1',
source: fs.readFileSync(new URL('../../../import-fixtures/mid1.cash', import.meta.url), { encoding: 'utf-8' }),
sourceFile: 'mid1.cash',
logs: [],
requires: [],
Expand All @@ -44,13 +47,17 @@ export const fixtures: Fixture[] = [
inputs: [{ name: 'a', type: 'int' }],
bytecode: '8b5393',
sourceMap: '3:11:3:18:1;:21::22:0;:11:::1',
source: fs.readFileSync(new URL('../../../import-fixtures/mid2.cash', import.meta.url), { encoding: 'utf-8' }),
sourceFile: 'mid2.cash',
logs: [],
requires: [],
inlineRanges: '0:0:leaf',
},
],
sources: {
'leaf.cash': readImportFixture('leaf.cash'),
'mid1.cash': readImportFixture('mid1.cash'),
'mid2.cash': readImportFixture('mid2.cash'),
},
inlineRanges: '1:3:m1;5:7:m2',
},
},
Expand Down Expand Up @@ -90,7 +97,6 @@ export const fixtures: Fixture[] = [
sourceMap: '2:11:2:16:1',
logs: [],
requires: [],
source: fs.readFileSync(new URL('../../../import-fixtures/leaf.cash', import.meta.url), { encoding: 'utf-8' }),
sourceFile: 'leaf.cash',
},
{
Expand All @@ -101,7 +107,6 @@ export const fixtures: Fixture[] = [
sourceMap: '3:11:3:18:1;;:21::22:0;:11:::1',
logs: [],
requires: [],
source: fs.readFileSync(new URL('../../../import-fixtures/mid1.cash', import.meta.url), { encoding: 'utf-8' }),
sourceFile: 'mid1.cash',
},
{
Expand All @@ -112,10 +117,14 @@ export const fixtures: Fixture[] = [
sourceMap: '3:11:3:18:1;;:21::22:0;:11:::1',
logs: [],
requires: [],
source: fs.readFileSync(new URL('../../../import-fixtures/mid2.cash', import.meta.url), { encoding: 'utf-8' }),
sourceFile: 'mid2.cash',
},
],
sources: {
'leaf.cash': readImportFixture('leaf.cash'),
'mid1.cash': readImportFixture('mid1.cash'),
'mid2.cash': readImportFixture('mid2.cash'),
},
},
fingerprint: '316a3305152ec0695bf80303736c79dd1f9cc2f1dbccf57d9965094401363307',
},
Expand Down
41 changes: 39 additions & 2 deletions packages/cashc/test/global-definitions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -291,9 +291,9 @@ describe('Inlining and shared definitions', () => {
expect(artifact.debug?.functions).toContainEqual(expect.objectContaining({
name: 'assertPositive',
sourceFile: 'helpers.cash',
source: importedSource,
requires: [expect.objectContaining({ line: 2, message: 'must be positive' })],
}));
expect(artifact.debug?.sources).toEqual({ 'helpers.cash': importedSource });
expect(artifact.debug?.functions?.[0].id).toBeUndefined();
expect(artifact.debug?.inlineRanges).toMatch(/^\d+:\d+:assertPositive$/);
});
Expand Down Expand Up @@ -368,9 +368,46 @@ describe('Global constants', () => {
expect(artifact.debug?.functions?.[0]).toMatchObject({
name: 'IMPORTED_HASH',
kind: 'constant',
source: importedSource,
sourceFile: 'constants.cash',
});
expect(artifact.debug?.sources).toEqual({ 'constants.cash': importedSource });
});

it('keeps the text of an imported file once, however many of its definitions are used', () => {
const importedSource = `
int constant FIRST = 1;
int constant SECOND = 2;
int constant THIRD = 3;
function addFirst(int a) returns (int) { return a + FIRST; }`;
const source = `
import "./constants.cash";
contract Imported(int x) {
function spend() {
require(addFirst(x) == SECOND + THIRD);
}
}`;

const artifact = compileString(source, { files: { './constants.cash': importedSource } });
const frames = artifact.debug?.functions ?? [];
expect(frames.map((frame) => frame.name).sort()).toEqual(['FIRST', 'SECOND', 'THIRD', 'addFirst']);
frames.forEach((frame) => {
expect(frame.sourceFile).toBe('constants.cash');
expect(frame).not.toHaveProperty('source');
});
expect(artifact.debug?.sources).toEqual({ 'constants.cash': importedSource });
expect(JSON.stringify(artifact).split('SECOND = 2').length - 1).toBe(1);
});

it('omits debug sources when nothing is imported', () => {
const source = `
int constant LOCAL = 7;
contract Local(int x) {
function spend() {
require(x == LOCAL);
}
}`;

expect(compileString(source).debug?.sources).toBeUndefined();
});

it('folds constant definitions to literals at compile time', () => {
Expand Down
3 changes: 3 additions & 0 deletions packages/cashc/test/import-fixtures/clash/mathlib/math.cash
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function localDouble(int a) returns (int) {
return a * 2;
}
8 changes: 8 additions & 0 deletions packages/cashc/test/import-fixtures/clash/nm_clash_main.cash
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import "mathlib/math.cash";
import "./mathlib/math.cash";

contract NodeModulesClash() {
function spend(int x) {
require(nmAdd(localDouble(x), 3) == 13);
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 35 additions & 0 deletions packages/cashc/test/imports.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,41 @@ describe('Imports from node_modules (package imports)', () => {
.toEqual(['mathlib/helper.cash', 'mathlib/main.cash']);
});

it('gives two different files that share a package path distinct provenance', () => {
// clash/nm_clash_main.cash imports the package mathlib/math.cash and a local ./mathlib/math.cash,
// whose short names are both 'mathlib/math.cash'; each must keep its own source in the artifact
const artifact = compileFile(fixture('clash/nm_clash_main.cash'), { disableInlining: true });
const frames = artifact.debug?.functions ?? [];
const sourceFileOf = (name: string): string | undefined => frames.find((frame) => frame.name === name)?.sourceFile;

expect(sourceFileOf('localDouble')).toEqual('mathlib/math.cash');
expect(sourceFileOf('nmAdd')).toEqual('../node_modules/mathlib/math.cash');
expect(artifact.debug?.sources).toEqual({
'mathlib/math.cash': readFixture('clash/mathlib/math.cash'),
'../node_modules/mathlib/math.cash': readFixture('node_modules/mathlib/math.cash'),
});
});

it('keeps provenance distinct when a renamed file would take another file\'s name', () => {
// A contract inside node_modules names even its local files by package path. Its ./lib/x.cash and bar's
// nested copy of foo/lib/x.cash both start as 'foo/lib/x.cash'; renaming the local one to 'lib/x.cash'
// then meets the package lib/x.cash, which is renamed in turn
const artifact = compileFile(fixture('clash/node_modules/foo/main.cash'), { disableInlining: true });
const frames = artifact.debug?.functions ?? [];
const sourceFileOf = (name: string): string | undefined => frames.find((frame) => frame.name === name)?.sourceFile;

expect(sourceFileOf('localCheck')).toEqual('lib/x.cash');
expect(sourceFileOf('nestedCheck')).toEqual('../bar/node_modules/foo/lib/x.cash');
expect(sourceFileOf('packageCheck')).toEqual('node_modules/lib/x.cash');
expect(sourceFileOf('viaBar')).toEqual('bar/y.cash');
expect(artifact.debug?.sources).toEqual({
'lib/x.cash': readFixture('clash/node_modules/foo/lib/x.cash'),
'../bar/node_modules/foo/lib/x.cash': readFixture('clash/node_modules/bar/node_modules/foo/lib/x.cash'),
'node_modules/lib/x.cash': readFixture('clash/node_modules/foo/node_modules/lib/x.cash'),
'bar/y.cash': readFixture('clash/node_modules/bar/y.cash'),
});
});

it('throws when a package import cannot be found in any node_modules directory', () => {
expect(() => compileFile(fixture('nm_missing_main.cash'))).toThrow(ImportResolutionError);
expect(() => compileFile(fixture('nm_missing_main.cash'))).toThrow(
Expand Down
3 changes: 2 additions & 1 deletion packages/cashscript/src/debug-frame.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
RequireStatement,
Script,
bytecodeToScript,
getDebugFrameSource,
parseAndResolveInlineRanges,
sourceMapToLocationData,
} from '@cashscript/utils';
Expand Down Expand Up @@ -68,7 +69,7 @@ const resolveFrameByBytecode = (artifact: Artifact, activeBytecode: string): Res

const resolveDebugFrame = (artifact: Artifact, frame: DebugFrame): ResolvedFrame => ({
sourceMap: frame.sourceMap,
source: frame.source ?? artifact.source,
source: getDebugFrameSource(artifact.debug?.sources, frame) ?? artifact.source,
sourceName: frame.sourceFile ?? `${artifact.contractName}.cash`,
ipOffset: 0, // function bodies have no constructor-arg prefix; their ips start at 0
requires: frame.requires,
Expand Down
Loading
Loading