Skip to content
This repository was archived by the owner on Apr 3, 2024. It is now read-only.

Commit 37399c2

Browse files
fix: Address compilation errors when using the agent with Typescript (#370)
1 parent d7bec41 commit 37399c2

File tree

12 files changed

+106
-41
lines changed

12 files changed

+106
-41
lines changed

package.json

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
"version": "2.3.0",
44
"author": "Google Inc.",
55
"description": "Stackdriver Debug Agent for Node.js",
6-
"main": "./build/src/index.js",
7-
"types": "./build/types/index.d.ts",
6+
"main": "./build/src/index",
7+
"types": "./build/src/index.d.ts",
88
"repository": "googlecloudplatform/cloud-debug-nodejs",
99
"keywords": [
1010
"google",
@@ -81,7 +81,7 @@
8181
"test": "./bin/run-test.sh",
8282
"check": "gts check",
8383
"clean": "gts clean",
84-
"precompile": "npm run compile-scripts && node build/scripts/setup-test.js",
84+
"precompile": "npm run compile-scripts && node build/scripts/setup-build-dir.js",
8585
"compile": "tsc -p .",
8686
"compile-scripts": "tsc -p scripts-tsconfig.json",
8787
"fix": "gts fix",
@@ -93,7 +93,6 @@
9393
"CHANGELOG.md",
9494
"LICENSE",
9595
"README.md",
96-
"build/src",
97-
"build/types"
96+
"build/src"
9897
]
9998
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,20 @@ const SYSTEM_TEST = 'system-test';
3232
const PROJECT_ROOT = path.join(__dirname, '..', '..');
3333
const BUILD_DIR = path.join(PROJECT_ROOT, 'build');
3434

35+
const INPUT_TYPES_DIR = path.join(PROJECT_ROOT, 'src', 'types');
36+
const OUTPUT_TYPES_DIR = path.join(BUILD_DIR, 'src', 'types');
37+
3538
const INPUT_TEST_DIR = path.join(PROJECT_ROOT, TEST);
3639
const INPUT_SYSTEM_TEST_DIR = path.join(PROJECT_ROOT, SYSTEM_TEST);
3740

3841
const OUTPUT_TEST_DIR = path.join(BUILD_DIR, TEST);
3942
const OUTPUT_SYSTEM_TEST_DIR = path.join(BUILD_DIR, SYSTEM_TEST);
4043

44+
async function copyTypes(): Promise<void> {
45+
await mkdirp(OUTPUT_TYPES_DIR);
46+
await ncp(INPUT_TYPES_DIR, OUTPUT_TYPES_DIR);
47+
}
48+
4149
async function setupUnitTests(): Promise<void> {
4250
await mkdirp(OUTPUT_TEST_DIR);
4351
await ncp(INPUT_TEST_DIR, OUTPUT_TEST_DIR);
@@ -50,6 +58,7 @@ async function setupSystemTests(): Promise<void> {
5058

5159
async function main(): Promise<void> {
5260
try {
61+
await copyTypes();
5362
await setupUnitTests();
5463
await setupSystemTests();
5564
}

src/agent/config.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@
1616

1717
import * as common from '../types/common';
1818

19-
export interface DebugAgentConfig extends common.AuthenticationConfig {
19+
export type DebugAgentConfig = {
20+
[K in keyof ResolvedDebugAgentConfig]?: Partial<ResolvedDebugAgentConfig[K]>
21+
};
22+
23+
export interface ResolvedDebugAgentConfig extends common.AuthenticationConfig {
2024
workingDirectory: string;
2125

2226
/**
@@ -166,7 +170,7 @@ export interface StackdriverConfig extends common.AuthenticationConfig {
166170
debug?: DebugAgentConfig;
167171
}
168172

169-
export const defaultConfig: DebugAgentConfig = {
173+
export const defaultConfig: ResolvedDebugAgentConfig = {
170174
// FIXME(ofrobots): presently this is dependent what cwd() is at the time this
171175
// file is first required. We should make the default config static.
172176
workingDirectory: process.cwd(),

src/agent/debuglet.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ import * as debugapi from './v8/debugapi';
4646
import * as assert from 'assert';
4747

4848
import * as stackdriver from '../types/stackdriver';
49-
import {DebugAgentConfig} from './config';
49+
import {DebugAgentConfig, ResolvedDebugAgentConfig} from './config';
5050
import {Debug, PackageInfo} from '../client/stackdriver/debug';
5151
import {Logger} from '../types/common';
5252
import {DebugApi} from './v8/debugapi';
@@ -179,7 +179,7 @@ export class Debuglet extends EventEmitter {
179179
isReadyManager: IsReady = new IsReadyImpl(this);
180180

181181
// Exposed for testing
182-
config: DebugAgentConfig;
182+
config: ResolvedDebugAgentConfig;
183183
fetcherActive: boolean;
184184
logger: Logger;
185185
debuggee: Debuggee|null;
@@ -244,7 +244,7 @@ export class Debuglet extends EventEmitter {
244244
this.debuggeeRegistered = new CachedPromise();
245245
}
246246

247-
static normalizeConfig_(config: DebugAgentConfig): DebugAgentConfig {
247+
static normalizeConfig_(config: DebugAgentConfig): ResolvedDebugAgentConfig {
248248
const envConfig = {
249249
logLevel: process.env.GCLOUD_DEBUG_LOGLEVEL,
250250
serviceContext: {

src/agent/state/inspector-state.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const isEmpty = lodash.isEmpty;
2727
import {StatusMessage} from '../../client/stackdriver/status-message';
2828

2929
import * as stackdriver from '../../types/stackdriver';
30-
import {DebugAgentConfig} from '../config';
30+
import {ResolvedDebugAgentConfig} from '../config';
3131
import {V8Inspector} from '../v8/v8inspector';
3232

3333
const assert = debugAssert(!!process.env.CLOUD_DEBUG_ASSERTIONS);
@@ -79,7 +79,7 @@ class StateResolver {
7979
private callFrames: inspector.Debugger.CallFrame[];
8080
private v8Inspector: V8Inspector;
8181
private expressions: string[]|undefined;
82-
private config: DebugAgentConfig;
82+
private config: ResolvedDebugAgentConfig;
8383
private scriptmapper: {[id: string]: {url: string}};
8484
private breakpoint: stackdriver.Breakpoint;
8585
private evaluatedExpressions: stackdriver.Variable[];
@@ -96,7 +96,7 @@ class StateResolver {
9696
*/
9797
constructor(
9898
callFrames: inspector.Debugger.CallFrame[],
99-
breakpoint: stackdriver.Breakpoint, config: DebugAgentConfig,
99+
breakpoint: stackdriver.Breakpoint, config: ResolvedDebugAgentConfig,
100100
scriptmapper: {[id: string]: {url: string}}, v8Inspector: V8Inspector) {
101101
this.callFrames = callFrames;
102102
this.breakpoint = breakpoint;
@@ -552,7 +552,7 @@ export function testAssert(): void {
552552
*/
553553
export function capture(
554554
callFrames: inspector.Debugger.CallFrame[],
555-
breakpoint: stackdriver.Breakpoint, config: DebugAgentConfig,
555+
breakpoint: stackdriver.Breakpoint, config: ResolvedDebugAgentConfig,
556556
scriptmapper: {[id: string]: {url: string}},
557557
v8Inspector: V8Inspector): stackdriver.Breakpoint {
558558
return (new StateResolver(

src/agent/state/legacy-state.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import {StatusMessage} from '../../client/stackdriver/status-message';
2727

2828
import * as v8 from '../../types/v8';
2929
import * as stackdriver from '../../types/stackdriver';
30-
import {DebugAgentConfig} from '../config';
30+
import {ResolvedDebugAgentConfig} from '../config';
3131

3232
// TODO: Determine if `ScopeType` should be named `scopeType`.
3333
// tslint:disable-next-line:variable-name
@@ -72,7 +72,7 @@ export function evaluate(expression: string, frame: v8.FrameMirror):
7272
class StateResolver {
7373
private state: v8.ExecutionState;
7474
private expressions: string[];
75-
private config: DebugAgentConfig;
75+
private config: ResolvedDebugAgentConfig;
7676
private ctx: v8.Debug;
7777
private evaluatedExpressions: stackdriver.Variable[];
7878
private totalSize: number;
@@ -88,7 +88,7 @@ class StateResolver {
8888
*/
8989
constructor(
9090
execState: v8.ExecutionState, expressions: string[],
91-
config: DebugAgentConfig, v8debug: v8.Debug) {
91+
config: ResolvedDebugAgentConfig, v8debug: v8.Debug) {
9292
this.state = execState;
9393
this.expressions = expressions;
9494
this.config = config;
@@ -565,7 +565,8 @@ export function testAssert(): void {
565565
*/
566566
export function capture(
567567
execState: v8.ExecutionState, expressions: string[],
568-
config: DebugAgentConfig, v8debug: v8.Debug): stackdriver.Breakpoint {
568+
config: ResolvedDebugAgentConfig,
569+
v8debug: v8.Debug): stackdriver.Breakpoint {
569570
return (new StateResolver(execState, expressions, config, v8debug))
570571
.capture_();
571572
}

src/agent/v8/inspector-debugapi.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import * as path from 'path';
2222
import {StatusMessage} from '../../client/stackdriver/status-message';
2323
import {Logger} from '../../types/common';
2424
import * as stackdriver from '../../types/stackdriver';
25-
import {DebugAgentConfig} from '../config';
25+
import {ResolvedDebugAgentConfig} from '../config';
2626
import {FileStats, ScanStats} from '../io/scanner';
2727
import {MapInfoOutput, SourceMapper} from '../io/sourcemapper';
2828
import * as state from '../state/inspector-state';
@@ -41,7 +41,7 @@ export class BreakpointData {
4141

4242
export class InspectorDebugApi implements debugapi.DebugApi {
4343
logger: Logger;
44-
config: DebugAgentConfig;
44+
config: ResolvedDebugAgentConfig;
4545
fileStats: ScanStats;
4646
breakpoints: {[id: string]: BreakpointData} = {};
4747
sourcemapper: SourceMapper;
@@ -60,7 +60,7 @@ export class InspectorDebugApi implements debugapi.DebugApi {
6060
numBreakpoints = 0;
6161
v8Inspector: V8Inspector;
6262
constructor(
63-
logger: Logger, config: DebugAgentConfig, jsFiles: ScanStats,
63+
logger: Logger, config: ResolvedDebugAgentConfig, jsFiles: ScanStats,
6464
sourcemapper: SourceMapper) {
6565
this.logger = logger;
6666
this.config = config;

src/agent/v8/legacy-debugapi.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import {StatusMessage} from '../../client/stackdriver/status-message';
2525
import {Logger} from '../../types/common';
2626
import * as stackdriver from '../../types/stackdriver';
2727
import * as v8 from '../../types/v8';
28-
import {DebugAgentConfig} from '../config';
28+
import {ResolvedDebugAgentConfig} from '../config';
2929
import {FileStats, ScanStats} from '../io/scanner';
3030
import {MapInfoOutput, SourceMapper} from '../io/sourcemapper';
3131
import * as state from '../state/legacy-state';
@@ -47,7 +47,7 @@ export class V8DebugApi implements debugapi.DebugApi {
4747
breakpoints: {[id: string]: V8BreakpointData} = {};
4848
sourcemapper: SourceMapper;
4949
v8: v8.Debug;
50-
config: DebugAgentConfig;
50+
config: ResolvedDebugAgentConfig;
5151
fileStats: ScanStats;
5252
listeners: {[id: string]: utils.Listener} = {};
5353
v8Version: RegExpExecArray|null;
@@ -60,7 +60,7 @@ export class V8DebugApi implements debugapi.DebugApi {
6060
numBreakpoints = 0;
6161

6262
constructor(
63-
logger: Logger, config: DebugAgentConfig, jsFiles: ScanStats,
63+
logger: Logger, config: ResolvedDebugAgentConfig, jsFiles: ScanStats,
6464
sourcemapper: SourceMapper) {
6565
this.sourcemapper = sourcemapper;
6666
this.v8 = vm.runInDebugContext('Debug');

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ let debuglet: Debuglet;
3535
* @example
3636
* debug.startAgent();
3737
*/
38-
export function start(options: DebugAgentConfig|StackdriverConfig): Debuglet|
38+
export function start(options?: DebugAgentConfig|StackdriverConfig): Debuglet|
3939
IsReady {
4040
options = options || {};
4141
const agentConfig: DebugAgentConfig =

system-test/test-install.ts

Lines changed: 62 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import * as assert from 'assert';
1818
import * as path from 'path';
1919

20-
import {globP, ncpP, rimrafP, spawnP, tmpDirP, writeFileP} from './utils';
20+
import {globP, mkdirP, ncpP, rimrafP, spawnP, tmpDirP, writeFileP} from './utils';
2121

2222
const INDEX_TS = 'index.ts';
2323
const INDEX_JS = 'index.js';
@@ -36,6 +36,36 @@ debug.start();`,
3636
code: `import * as debug from '@google-cloud/debug-agent';
3737
debug.start({ allowExpressions: true });`,
3838
description: 'imports the module and starts with {allowExpressions: true}'
39+
},
40+
{
41+
code: `import * as debug from '@google-cloud/debug-agent';
42+
debug.start({
43+
allowExpressions: true,
44+
serviceContext: {
45+
service: 'Some service'
46+
}
47+
});`,
48+
description: 'imports the module and starts with a partial `serviceContext`'
49+
},
50+
{
51+
code: `import * as debug from '@google-cloud/debug-agent';
52+
debug.start({
53+
allowExpressions: true,
54+
serviceContext: {
55+
service: 'Some service',
56+
version: 'Some version'
57+
}
58+
});`,
59+
description: 'imports the module and starts with a complete `serviceContext`'
60+
},
61+
{
62+
code: `import * as debug from '@google-cloud/debug-agent';
63+
debug.start({
64+
capture: {
65+
maxFrames: 1
66+
}
67+
});`,
68+
description: 'imports the module and starts with a partial `capture`'
3969
}
4070
];
4171

@@ -74,6 +104,17 @@ interface CodeSample {
74104

75105
describe('Installation', () => {
76106
let installDir: string|undefined;
107+
before(async () => {
108+
const tgz = await globP(`${process.cwd()}/*.tgz`);
109+
assert.deepStrictEqual(
110+
tgz.length, 0,
111+
`Expected zero tgz files in the current working directory before ` +
112+
`running the test but found files: ${tgz.map(file => {
113+
const parts = file.split(path.sep);
114+
return parts[parts.length - 1];
115+
})}`);
116+
});
117+
77118
beforeEach(async function() {
78119
this.timeout(TIMEOUT_MS);
79120
// This script assumes that you don't already have a TGZ file
@@ -103,17 +144,26 @@ describe('Installation', () => {
103144

104145
describe('When used with Typescript code', () => {
105146
TS_CODE_ARRAY.forEach((sample) => {
106-
it(`should install and work with code that ${sample.description}`,
107-
async function() {
108-
this.timeout(TIMEOUT_MS);
109-
assert(installDir);
110-
await writeFileP(
111-
path.join(installDir!, INDEX_TS), sample.code, 'utf-8');
112-
await spawnP(
113-
`node_modules${path.sep}.bin${path.sep}tsc`, [INDEX_TS],
114-
{cwd: installDir, stdio}, log);
115-
await spawnP('node', [INDEX_JS], {cwd: installDir, stdio}, log);
116-
});
147+
it.only(
148+
`should install and work with code that ${sample.description}`,
149+
async function() {
150+
this.timeout(TIMEOUT_MS);
151+
assert(installDir);
152+
const srcDir = path.join(installDir!, 'src');
153+
await mkdirP(srcDir);
154+
await writeFileP(path.join(srcDir, INDEX_TS), sample.code, 'utf-8');
155+
await spawnP(
156+
'npm', ['install', '--save-dev', 'gts', '[email protected]'],
157+
{cwd: installDir, stdio}, log);
158+
await spawnP(
159+
'gts', ['init', '--yes'], {cwd: installDir, stdio}, log);
160+
await spawnP(
161+
'npm', ['run', 'compile'], {cwd: installDir, stdio}, log);
162+
const buildDir = path.join(installDir!, 'build');
163+
await spawnP(
164+
'node', [path.join(buildDir, 'src', INDEX_JS)],
165+
{cwd: installDir, stdio}, log);
166+
});
117167
});
118168
});
119169

0 commit comments

Comments
 (0)