Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

feat: faster, lazy-friendly runtime loading #4181

Merged
merged 9 commits into from
Jul 25, 2023
65 changes: 65 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,71 @@ Each one of these scripts can be executed either from the root of the repo using
`npx lerna run <script> --scope <package>` or from individual modules using
`yarn <script>`.

#### Reproducting Bugs (Test-Driven Solving)

Troubleshooting bugs usually starts with adding a new test that demonstrates the
faulty behavior, then modifying implementations until the test passes.

The `jsii-calc` and `@scope/*` packages are used to test expected brhavior from
the compiler (note that the [aws/jsii-compiler](github.com/aws/jsii-compiler)
repository as a separate copy of these under the `fixtures` directory), as well
as downstream tooling (`jsii-pacmak`, `jsii-rosetta`, etc...). Each language
runtime has its own test suite, within which is a _compliance_ suite that tests
the same behaviors in all languages, and which should contain tests related to
behavior that isn't strictly specific to the given language.

The `yarn test:update` script in each package runs all tests and updates
snapshots when necessary. It is usually necessary to run this script at least in
`jsii-pacmak` and `jsii-reflect` after changing code in the `jsii-calc` or
`@scope/*` packages.

#### Debugging runtime behavior

Cross-language runtime behavior can be challenging to debug, as data is passed
across process boundaries through Inter-Process Communication (IPC) channels.
Further complicating things, the `@jsii/runtime` library packaged in the various
language runtimes is bundled (by `webpack`), which can make the Javascript
runtime code more complicated to follow.

Setting various environment variables can help understanding what is happening
better:

- `JSII_DEBUG=1` turns on verbose debug logging, which will cause the program to
emit extensive IPC tracing information to `STDERR`. This information can help
identify where things start to behave in unexpected ways, but can be a little
difficult to digest... One may want to refer to the [kernel API][kernel-api]
documentation to make sense of those traces.

- `JSII_DEBUG_TIMING=1` turns on specific timing information from the
`@jsii/kernel` high level API processing, which can be useful to narrow down
the possible causes for performance issues.

- `JSII_RUNTIME` can be set to point to the `bin/jsii-runtime` script within the
`@jsii/runtime` package in order to use a local, non-`webpack`ed version of
the runtime program. This can be particularly helpful when trying to diagnose
a problem within a debugger session.

- `NODE_OPTIONS` can be used to configure specific behaviors of the underlying
`node` runtime, such as specifying `--inspect-brk` to cause the node process
to wait for a debugger to attach before proceeding. This is useful to attach
Node dev tools to the runtime as it starts in order to use its debugger.

The [Visual Studio Code](https://code.visualstudio.com) _JavaScript Debug
Terminal_ feature can be particularly useful paired with appropriate
`JSII_RUNTIME` setting to run arbitrary jsii programs, automatically attaching
the VSCode debugger at startup. These terminals inject a specially crafted
`NODE_OPTIONS` variable that allows the VSCode debugger to consistently attach
to all `node` processes spawned within its context, including child processes
(which can be problematic when running with `--inspect-brk`, as the default
debugger interface's port can only be used by one process at a time).

Finally, the `debugger` Javascript statement can be added anywhere in the
runtime code or tested libraries in order to cause debuggers (if attached) to
pause. This can be easier (and more reliable) to set up than traditional
conditional break points.

[kernel-api]: https://aws.github.io/jsii/specification/3-kernel-api/

#### Linting & Formatting

Eslint and Prettier are used to lint and format our typescript code. The `lint`
Expand Down
61 changes: 53 additions & 8 deletions packages/@jsii/kernel/src/kernel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ export class Kernel {
* Set to true for timing data to be emitted.
*/
public debugTimingEnabled = false;
/**
* Set to true to validate assemblies upon loading (slow).
*/
public validateAssemblies = false;

readonly #assemblies = new Map<string, Assembly>();
readonly #objects = new ObjectTable(this.#typeInfoForFqn.bind(this));
Expand All @@ -69,6 +73,7 @@ export class Kernel {
this.#serializerHost = {
objects: this.#objects,
debug: this.#debug.bind(this),
isVisibleType: this.#isVisibleType.bind(this),
findSymbol: this.#findSymbol.bind(this),
lookupType: this.#typeInfoForFqn.bind(this),
};
Expand Down Expand Up @@ -147,10 +152,10 @@ export class Kernel {
}

// read .jsii metadata from the root of the package
let assmSpec;
let assmSpec: spec.Assembly;
try {
assmSpec = this.#debugTime(
() => spec.loadAssemblyFromPath(packageDir),
() => spec.loadAssemblyFromPath(packageDir, this.validateAssemblies),
`loadAssemblyFromPath(${packageDir})`,
);
} catch (e: any) {
Expand All @@ -159,10 +164,15 @@ export class Kernel {
);
}

// We do a `require.resolve` call, as otherwise, requiring with a directory will cause any `exports` from
// `package.json` to be ignored, preventing injection of a "lazy index" entry point.
const entryPoint = this.#require!.resolve(assmSpec.name, {
paths: [this.#installDir!],
});
mrgrain marked this conversation as resolved.
Show resolved Hide resolved
// load the module and capture its closure
const closure = this.#debugTime(
() => this.#require!(packageDir),
`require(${packageDir})`,
() => this.#require!(entryPoint),
`require(${entryPoint})`,
);
const assm = new Assembly(assmSpec, closure);
this.#debugTime(
Expand Down Expand Up @@ -333,7 +343,7 @@ export class Kernel {
throw new JsiiFault(`${method} is an async method, use "begin" instead`);
}

const fqn = jsiiTypeFqn(obj);
const fqn = jsiiTypeFqn(obj, this.#isVisibleType.bind(this));
const ret = this.#ensureSync(
`method '${objref[TOKEN_REF]}.${method}'`,
() => {
Expand Down Expand Up @@ -420,7 +430,7 @@ export class Kernel {
throw new JsiiFault(`Method ${method} is expected to be an async method`);
}

const fqn = jsiiTypeFqn(obj);
const fqn = jsiiTypeFqn(obj, this.#isVisibleType.bind(this));

const promise = fn.apply(
obj,
Expand Down Expand Up @@ -568,6 +578,21 @@ export class Kernel {
#addAssembly(assm: Assembly) {
this.#assemblies.set(assm.metadata.name, assm);

// We can use jsii runtime type information from jsii 1.19.0 onwards... Note that a version of
// 0.0.0 means we are assessing against a development tree, which is newer...
const jsiiVersion = assm.metadata.jsiiVersion.split(' ', 1)[0];
const [jsiiMajor, jsiiMinor, _jsiiPatch, ..._rest] = jsiiVersion
.split('.')
.map((str) => parseInt(str, 10));
if (
jsiiVersion === '0.0.0' ||
jsiiMajor > 1 ||
(jsiiMajor === 1 && jsiiMinor >= 19)
) {
this.#debug('Using compiler-woven runtime type information!');
return;
}

// add the __jsii__.fqn property on every constructor. this allows
// traversing between the javascript and jsii worlds given any object.
for (const fqn of Object.keys(assm.metadata.types ?? {})) {
Expand Down Expand Up @@ -869,7 +894,7 @@ export class Kernel {
methodInfo: spec.Method,
) {
const methodName = override.method;
const fqn = jsiiTypeFqn(obj);
const fqn = jsiiTypeFqn(obj, this.#isVisibleType.bind(this));
const methodContext = `${methodInfo.async ? 'async ' : ''}method${
fqn ? `${fqn}#` : methodName
}`;
Expand Down Expand Up @@ -1029,7 +1054,7 @@ export class Kernel {
return curr;
}

#typeInfoForFqn(fqn: string): spec.Type {
#typeInfoForFqn(fqn: spec.FQN): spec.Type {
const components = fqn.split('.');
const moduleName = components[0];

Expand All @@ -1047,6 +1072,26 @@ export class Kernel {
return fqnInfo;
}

/**
* Determines whether the provided FQN corresponds to a valid, exported type
* from any currently loaded assembly.
*
* @param fqn the tested FQN.
*
* @returns `true` IIF the FQN corresponds to a know exported type.
*/
#isVisibleType(fqn: spec.FQN): boolean {
try {
/* ignored */ this.#typeInfoForFqn(fqn);
return true;
} catch (e) {
if (e instanceof JsiiFault) {
return false;
}
throw e;
}
}

#typeInfoForMethod(
methodName: string,
fqn: string,
Expand Down
24 changes: 22 additions & 2 deletions packages/@jsii/kernel/src/link.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
import { copyFileSync, linkSync, mkdirSync, readdirSync, statSync } from 'fs';
import { join } from 'path';
import {
copyFileSync,
linkSync,
mkdirSync,
readdirSync,
statSync,
symlinkSync,
} from 'fs';
import { dirname, join } from 'path';

/**
* If `node` is started with `--preserve-symlinks`, the module loaded will
* preserve symbolic links instead of resolving them, making it possible to
* symbolically link packages in place instead of fully copying them.
*/
const PRESERVE_SYMLINKS = process.execArgv.includes('--preserve-symlinks');

/**
* Creates directories containing hard links if possible, and falls back on
Expand All @@ -9,6 +23,12 @@ import { join } from 'path';
* @param destination is the new file or directory to create.
*/
export function link(existing: string, destination: string): void {
if (PRESERVE_SYMLINKS) {
mkdirSync(dirname(destination), { recursive: true });
symlinkSync(existing, destination);
return;
}

const stat = statSync(existing);
if (!stat.isDirectory()) {
try {
Expand Down
Loading