Skip to content
Merged
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
6 changes: 4 additions & 2 deletions rust/contracts/rust-core-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -4132,11 +4132,13 @@ containing `target.path` when the configuration binds it to `target.directory`
(an adapter such as Delve that builds a package; a path without a directory is
`debug.target.invalid` with `field: "target.path"`); or from
`target.executable` (an absolute path the host found) when the configuration
binds it to `target.executable`. Each `adapterArguments` path is filled the
binds it to `target.executable`. `cwd` is filled from `target.workingDirectory`,
or from the directory containing `target.path` when the configuration binds it
to `target.directory`. Each `adapterArguments` path is filled the
same way, from `target.path`, the directory containing it (`target.directory`)
or `target.workingDirectory`, after `env`, so `env.<variable>` adds a variable
to the bound environment and leaves one the target sets; a target without the
fact is
fact a `cwd` or `adapterArguments` binding reads is
`debug.target.invalid` with `field: "target.path"` or
`"target.workingDirectory"`. Refusals use the `rejected` shape with
`debug.provider.stale`, `debug.request.unsupported`, `debug.runtime.stale`, or
Expand Down
60 changes: 59 additions & 1 deletion rust/crates/core/src/tests/declarative_plugins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2181,10 +2181,12 @@ fn debug_start_arguments_fill_adapter_arguments_from_path_facts() {
("/work", "/work/cmd/app/main.go", "/work/cmd/app")
};
// Delve's shape: it builds in `dlvCwd`, not in its own working directory,
// and runs the program there when `cwd` is left out.
// and runs the program in `cwd`, which the debug engine sets to the
// workspace root when nothing binds it.
let mut manifest = fixture("debug-adapter.plugin.json");
manifest["debugAdapters"][0]["configurations"]["launch"]["targetBindings"] = json!({
"program": "target.directory",
"cwd": "target.directory",
"adapterArguments": {
"dlvCwd": "target.directory",
"file": "target.path",
Expand All @@ -2210,6 +2212,7 @@ fn debug_start_arguments_fill_adapter_arguments_from_path_facts() {
json!({
"stopOnEntry": false,
"program": directory,
"cwd": directory,
"dlvCwd": directory,
"file": path,
"root": root,
Expand All @@ -2229,6 +2232,61 @@ fn debug_start_arguments_fill_adapter_arguments_from_path_facts() {
);
}

#[test]
fn debug_start_arguments_bind_cwd_to_the_directory_of_the_target() {
let (root, path, directory) = if cfg!(windows) {
(r"C:\work", r"C:\work\lab\main.go", r"C:\work\lab")
} else {
("/work", "/work/lab/main.go", "/work/lab")
};
let start = |cwd: &str, target: Value| {
let mut manifest = fixture("debug-adapter.plugin.json");
manifest["debugAdapters"][0]["configurations"]["launch"]["targetBindings"] =
json!({ "cwd": cwd });
lithe_plugin_manifest::debug_start_arguments_command(json!({
"manifest": manifest,
"adapterID": "example",
"request": "launch",
"facts": {
"registration": { "generation": 3, "currentGeneration": 3 },
"target": target,
},
}))
.expect("payload is well formed")
};
let target = json!({ "kind": "file", "path": path, "workingDirectory": root });
for (cwd, expected) in [
("target.directory", directory),
("target.workingDirectory", root),
] {
let launch = start(cwd, target.clone());
assert_eq!(launch["status"], "ok", "{launch}");
assert_eq!(launch["arguments"]["cwd"], expected, "{cwd}: {launch}");
}

// A bare file name has no directory to run in, and a target without a
// working directory has none either; both are refused before the start.
for (cwd, target, field) in [
(
"target.directory",
json!({ "kind": "file", "path": "main.go", "workingDirectory": root }),
"target.path",
),
(
"target.workingDirectory",
json!({ "kind": "file", "path": path }),
"target.workingDirectory",
),
] {
let refused = start(cwd, target);
assert_eq!(
refused["error"]["code"], "debug.target.invalid",
"{refused}"
);
assert_eq!(refused["error"]["field"], field, "{refused}");
}
}

#[test]
fn debug_start_arguments_add_an_adapter_argument_to_the_environment() {
let (root, path, directory) = if cfg!(windows) {
Expand Down
4 changes: 3 additions & 1 deletion rust/crates/debug/src/adapters/declaration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,9 @@ pub struct DebugTargetBindings {
/// Source of the `processId` argument: only `target.processID`. Attach only.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub process_id: Option<String>,
/// Source of the `cwd` argument: only `target.workingDirectory`.
/// Source of the `cwd` argument: `target.workingDirectory`, or
/// `target.directory` (the directory containing `target.path`) for an
/// adapter whose program runs where its package is, such as Delve.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub cwd: Option<String>,
/// Where a remote `host:port` goes. Attach only.
Expand Down
13 changes: 5 additions & 8 deletions rust/crates/debug/src/adapters/plan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,15 +147,12 @@ pub fn plan(
if bindings.process_id.is_some() && target.process_id.is_none() {
return Err(missing("target.processID", "process ID"));
}
if bindings.cwd.is_some()
&& target
.working_directory
.as_deref()
.is_none_or(str::is_empty)
// `cwd` and the adapter's own arguments all read path facts.
for fact in bindings
.cwd
.iter()
.chain(bindings.adapter_arguments.iter().flat_map(BTreeMap::values))
{
return Err(missing("target.workingDirectory", "working directory"));
}
for fact in bindings.adapter_arguments.iter().flat_map(BTreeMap::values) {
if path_fact(target, fact).is_some() {
continue;
}
Expand Down
10 changes: 7 additions & 3 deletions rust/crates/debug/src/adapters/start.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,11 @@ fn bound_arguments(
bind(
"cwd",
&bindings.cwd,
target.working_directory.clone().map(Value::from),
bindings
.cwd
.as_deref()
.and_then(|fact| path_fact(target, fact))
.map(Value::from),
);
bind(
"args",
Expand Down Expand Up @@ -205,8 +209,8 @@ pub(crate) fn target_directory(path: &str) -> Option<&str> {
.filter(|directory| !directory.is_empty())
}

/// The value of a path fact an `adapterArguments` binding reads, or `None`
/// when the target does not carry it.
/// The value of a path fact a `cwd` or `adapterArguments` binding reads, or
/// `None` when the target does not carry it.
pub(crate) fn path_fact<'a>(target: &'a TargetFacts, fact: &str) -> Option<&'a str> {
let present = |value: &'a Option<String>| value.as_deref().filter(|value| !value.is_empty());
match fact {
Expand Down
6 changes: 5 additions & 1 deletion rust/crates/debug/src/adapters/validate_configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,11 @@ impl DebugConfiguration {
&["target.path", TARGET_DIRECTORY, TARGET_EXECUTABLE][..],
),
("processId", &bindings.process_id, &["target.processID"][..]),
("cwd", &bindings.cwd, &["target.workingDirectory"][..]),
(
"cwd",
&bindings.cwd,
&["target.workingDirectory", TARGET_DIRECTORY][..],
),
("args", &bindings.args, &["target.arguments"][..]),
("env", &bindings.env, &["target.environment"][..]),
] {
Expand Down
4 changes: 2 additions & 2 deletions shared/plugins/contract/v1/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2350,8 +2350,8 @@ adapter serves `attach`), and one `onDebug:<id>` activation event per adapter.
| `requests` | `launch` (default) and/or `attach`. |
| `selectors[]` | Optional automatic matching by `targetKinds` and `languageIDs`; absent means explicit selection only. |
| `configurationTypes` | Imported `launch.json` `type` values the adapter answers to. |
| `configurations.launch/attach` | Default DAP `arguments`, a packaged `parametersSchema`, and `targetBindings` (`program` ← `target.path`, or ← `target.directory`, the directory containing `target.path`, for an adapter that debugs a package such as Delve, `processId` ← `target.processID`, `cwd` ← `target.workingDirectory`, and for `attach` only `connection` ← `target.host`, `target.port`, `target.pathMappings`; also bounded `args` ← `target.arguments` and `env` ← `target.environment`; see [Remote attach](#remote-attach)). A configuration for an undeclared request is refused. |
| `targetBindings.adapterArguments` | Up to 8 DAP arguments only this adapter defines, as `{"<argument path>": "<fact>"}` with the fact `target.path`, `target.directory` or `target.workingDirectory`. A path is dotted like a `connection` destination: up to 4 names, each a letter or `_` followed by up to 63 letters, digits, `_` or `-`, and objects on the way are created. For example Delve builds the program in `dlvCwd`, not in its own working directory, which the host sets to the workspace root, and `go build` reads the directory from `PWD`, which Delve leaves at that root; so a Go plugin binds `{"dlvCwd": "target.directory", "env.PWD": "target.directory"}`. Inside `env` a path names one variable and adds it to the environment the configuration or the `env` binding gives; elsewhere a path may not start at `program`, `processId`, `cwd`, `args` or `env`, which have their own bindings, or at `noDebug` or a `__` name, which belong to the host. Two paths may not overlap, nor a path and a `connection` destination, and a path may not land on or under a value the configuration's own `arguments` set, which the binding could never fill. Like every binding it fills the argument only when nothing holds it, so a variable the target's environment sets stays, and a target without the fact is refused with `debug.target.invalid`. |
| `configurations.launch/attach` | Default DAP `arguments`, a packaged `parametersSchema`, and `targetBindings` (`program` ← `target.path`, or ← `target.directory`, the directory containing `target.path`, for an adapter that debugs a package such as Delve, `processId` ← `target.processID`, `cwd` ← `target.workingDirectory`, or ← `target.directory` for an adapter whose program runs where its package is, and for `attach` only `connection` ← `target.host`, `target.port`, `target.pathMappings`; also bounded `args` ← `target.arguments` and `env` ← `target.environment`; see [Remote attach](#remote-attach)). A configuration for an undeclared request is refused. |
| `targetBindings.adapterArguments` | Up to 8 DAP arguments only this adapter defines, as `{"<argument path>": "<fact>"}` with the fact `target.path`, `target.directory` or `target.workingDirectory`. A path is dotted like a `connection` destination: up to 4 names, each a letter or `_` followed by up to 63 letters, digits, `_` or `-`, and objects on the way are created. For example Delve builds the program in `dlvCwd`, not in its own working directory, which the host sets to the workspace root, and `go build` reads the directory from `PWD`, which Delve leaves at that root; so a Go plugin binds `{"dlvCwd": "target.directory", "env.PWD": "target.directory"}` here, and `cwd` ← `target.directory` so the program also runs in its package directory rather than in the workspace root the debug engine gives a launch without `cwd`. Inside `env` a path names one variable and adds it to the environment the configuration or the `env` binding gives; elsewhere a path may not start at `program`, `processId`, `cwd`, `args` or `env`, which have their own bindings, or at `noDebug` or a `__` name, which belong to the host. Two paths may not overlap, nor a path and a `connection` destination, and a path may not land on or under a value the configuration's own `arguments` set, which the binding could never fill. Like every binding it fills the argument only when nothing holds it, so a variable the target's environment sets stays, and a target without the fact is refused with `debug.target.invalid`. |
| `configurations.launch.executable` | For a target that is a source file the adapter cannot debug directly (C, C++, Swift): `setting` names a `file` setting of the plugin, `candidates[]` (up to 8) are the paths a host looks for when the user chose nothing. Each candidate starts with `${workspaceFolder}/` or `${fileDirectory}/`, may use `${fileBasenameNoExtension}`, and has no `..`. Declare it together with `targetBindings.program: "target.executable"` and neither without the other; a launch fills `program` with the path the host settled on. The user's setting always wins: a chosen path that is not a runnable file is an error, not a reason to try the candidates. The host shows the path it found in that setting, and the setting's `description` is where the plugin teaches how to produce the program. |
| `capabilities.disabled` | Standard DAP `supports*` names or `exceptionBreakpointFilters` the host treats as absent. |
| `features` | Adapter-specific DAP extensions the adapter implements; see [Adapter features and defaults](#adapter-features-and-defaults). |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@
"edits": [{"op": "set", "path": "/block/debugAdapters/0/configurations", "value": {"launch": {"targetBindings": {"program": "target.processID"}}}}],
"expected": {"code": "plugin.manifest.invalid", "field": "debugAdapters[0].configurations.launch.targetBindings.program"}
},
{
"id": "cwd-from-target-path",
"layer": "schema",
"edits": [{"op": "set", "path": "/block/debugAdapters/0/configurations", "value": {"launch": {"targetBindings": {"cwd": "target.path"}}}}],
"expected": {"code": "plugin.manifest.invalid", "field": "debugAdapters[0].configurations.launch.targetBindings.cwd"}
},
{
"id": "function-for-step-back",
"layer": "schema",
Expand Down
3 changes: 2 additions & 1 deletion shared/plugins/contract/v1/plugin-manifest.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -698,7 +698,8 @@
"const": "target.processID"
},
"cwd": {
"const": "target.workingDirectory"
"description": "`target.workingDirectory` is the working directory. `target.directory` is the directory containing `target.path`, for adapters whose program runs where its package is.",
"enum": ["target.workingDirectory", "target.directory"]
},
"args": { "const": "target.arguments" },
"env": { "const": "target.environment" },
Expand Down
Loading
Loading