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

debug: respect configured build tags/flags #1790

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
59 changes: 0 additions & 59 deletions src/goDebugConfiguration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,28 +220,6 @@ export class GoDebugConfigurationProvider implements vscode.DebugConfigurationPr
debugConfiguration['cwd'] = resolveHomeDir(debugConfiguration['cwd']);
}

// Remove any '--gcflags' entries and show a warning
if (debugConfiguration['buildFlags']) {
const resp = this.removeGcflags(debugConfiguration['buildFlags']);
if (resp.removed) {
debugConfiguration['buildFlags'] = resp.args;
this.showWarning(
'ignoreDebugGCFlagsWarning',
"User specified build flag '--gcflags' in 'buildFlags' is being ignored (see [debugging with build flags](https://github.com/golang/vscode-go/blob/master/docs/debugging.md#specifying-other-build-flags) documentation)"
);
}
}
if (debugConfiguration['env'] && debugConfiguration['env']['GOFLAGS']) {
const resp = this.removeGcflags(debugConfiguration['env']['GOFLAGS']);
if (resp.removed) {
debugConfiguration['env']['GOFLAGS'] = resp.args;
this.showWarning(
'ignoreDebugGCFlagsWarning',
"User specified build flag '--gcflags' in 'GOFLAGS' is being ignored (see [debugging with build flags](https://github.com/golang/vscode-go/blob/master/docs/debugging.md#specifying-other-build-flags) documentation)"
);
}
}

const dlvToolPath = getBinPath(debugAdapter);
if (!path.isAbsolute(dlvToolPath)) {
// If user has not already declined to install this tool,
Expand Down Expand Up @@ -324,43 +302,6 @@ export class GoDebugConfigurationProvider implements vscode.DebugConfigurationPr
return debugConfiguration;
}

public removeGcflags(args: string): { args: string; removed: boolean } {
// From `go help build`
// ...
// -gcflags '[pattern=]arg list'
// arguments to pass on each go tool compile invocation.
//
// The -asmflags, -gccgoflags, -gcflags, and -ldflags flags accept a
// space-separated list of arguments to pass to an underlying tool
// during the build. To embed spaces in an element in the list, surround
// it with either single or double quotes. The argument list may be
// preceded by a package pattern and an equal sign, which restricts
// the use of that argument list to the building of packages matching
// that pattern (see 'go help packages' for a description of package
// patterns). Without a pattern, the argument list applies only to the
// packages named on the command line. The flags may be repeated
// with different patterns in order to specify different arguments for
// different sets of packages. If a package matches patterns given in
// multiple flags, the latest match on the command line wins.
// For example, 'go build -gcflags=-S fmt' prints the disassembly
// only for package fmt, while 'go build -gcflags=all=-S fmt'
// prints the disassembly for fmt and all its dependencies.

// Regexp Explanation:
// 1. (^|\s): the flag is preceded by a white space or is at the start of the line.
// 2. -gcflags: the name of the flag.
// 3. (=| ): the name of the flag is followed by = or a space.
// 4. ('[^']*'|"[^"]*"|[^'"\s]+)+: the value of the flag is a combination of nonwhitespace
// characters and quoted strings which may contain white space.
const gcflagsRegexp = /(^|\s)(-gcflags)(=| )('[^']*'|"[^"]*"|[^'"\s]+)+/;
let removed = false;
while (args.search(gcflagsRegexp) >= 0) {
args = args.replace(gcflagsRegexp, '');
removed = true;
}
return { args, removed };
}

public resolveDebugConfigurationWithSubstitutedVariables(
folder: vscode.WorkspaceFolder | undefined,
debugConfiguration: vscode.DebugConfiguration,
Expand Down
59 changes: 0 additions & 59 deletions test/integration/goDebugConfiguration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -369,65 +369,6 @@ suite('Debug Configuration Modify User Config', () => {
const debugConfigProvider = new GoDebugConfigurationProvider();

suite('remove gcflags', () => {
test('remove gcflags from string args', () => {
const tt = [
{
input: '-gcflags=all=-l',
want: { args: '', removed: true }
},
{
input: '-gcflags all=-l',
want: { args: '', removed: true }
},
// Preserve other flags
{
input: '-race -gcflags=all=-l -mod=mod',
want: { args: '-race -mod=mod', removed: true }
},
{
input: '-race -gcflags all=-l -mod=mod',
want: { args: '-race -mod=mod', removed: true }
},
// Test with quoted value
{
input: "-mod=mod -gcflags=test/...='hello goodbye' -race",
want: { args: '-mod=mod -race', removed: true }
},
{
input: '-mod=mod -gcflags test/...="hello goodbye" -race',
want: { args: '-mod=mod -race', removed: true }
},
{
input: "-mod=mod -gcflags='test/...=hello goodbye' -race",
want: { args: '-mod=mod -race', removed: true }
},
{
input: '-mod=mod -gcflags "test/...=hello goodbye" -race',
want: { args: '-mod=mod -race', removed: true }
},
// Multiple -gcflags present
{
input: '-mod=mod -gcflags "test/...=hello goodbye" -race -gcflags=all="hello goodbye"',
want: { args: '-mod=mod -race', removed: true }
},
// No gcflags are present
{
input: '',
want: { args: '', removed: false }
},
{
input: '-race -mod=gcflags',
want: { args: '-race -mod=gcflags', removed: false }
}
];

tt.forEach((tc) => {
const got = debugConfigProvider.removeGcflags(tc.input);

assert.strictEqual(got.args, tc.want.args, `args for ${tc.input} do not match expected`);
assert.strictEqual(got.removed, tc.want.removed, `removed for ${tc.input} does not match expected`);
});
});
test('remove user set -gcflags in buildFlags', () => {
const config = {
name: 'Launch',
Expand Down