Skip to content

Commit ef87061

Browse files
committed
feat(debug): targetBindings.cwd 可以绑到 target.directory
调试引擎在 launch 参数缺 cwd 时会把 cwd 填成工作区根目录, 多项目工作区里 Delve 启动的程序因此在根目录运行,按包目录读的相对路径文件找不到。 cwd 绑定新增 target.directory:被调程序在源文件所在目录运行。 同步 schema、契约 README、指南、参考页与协议更新记录(#56 新行),补测试和拒绝 fixture。 #55 合入时没有带上这个提交,从最新 main 重新提交。
1 parent 4a2ecb3 commit ef87061

12 files changed

Lines changed: 100 additions & 25 deletions

File tree

‎rust/contracts/rust-core-api.md‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4132,11 +4132,13 @@ containing `target.path` when the configuration binds it to `target.directory`
41324132
(an adapter such as Delve that builds a package; a path without a directory is
41334133
`debug.target.invalid` with `field: "target.path"`); or from
41344134
`target.executable` (an absolute path the host found) when the configuration
4135-
binds it to `target.executable`. Each `adapterArguments` path is filled the
4135+
binds it to `target.executable`. `cwd` is filled from `target.workingDirectory`,
4136+
or from the directory containing `target.path` when the configuration binds it
4137+
to `target.directory`. Each `adapterArguments` path is filled the
41364138
same way, from `target.path`, the directory containing it (`target.directory`)
41374139
or `target.workingDirectory`, after `env`, so `env.<variable>` adds a variable
41384140
to the bound environment and leaves one the target sets; a target without the
4139-
fact is
4141+
fact a `cwd` or `adapterArguments` binding reads is
41404142
`debug.target.invalid` with `field: "target.path"` or
41414143
`"target.workingDirectory"`. Refusals use the `rejected` shape with
41424144
`debug.provider.stale`, `debug.request.unsupported`, `debug.runtime.stale`, or

‎rust/crates/core/src/tests/declarative_plugins.rs‎

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2181,10 +2181,12 @@ fn debug_start_arguments_fill_adapter_arguments_from_path_facts() {
21812181
("/work", "/work/cmd/app/main.go", "/work/cmd/app")
21822182
};
21832183
// Delve's shape: it builds in `dlvCwd`, not in its own working directory,
2184-
// and runs the program there when `cwd` is left out.
2184+
// and runs the program in `cwd`, which the debug engine sets to the
2185+
// workspace root when nothing binds it.
21852186
let mut manifest = fixture("debug-adapter.plugin.json");
21862187
manifest["debugAdapters"][0]["configurations"]["launch"]["targetBindings"] = json!({
21872188
"program": "target.directory",
2189+
"cwd": "target.directory",
21882190
"adapterArguments": {
21892191
"dlvCwd": "target.directory",
21902192
"file": "target.path",
@@ -2210,6 +2212,7 @@ fn debug_start_arguments_fill_adapter_arguments_from_path_facts() {
22102212
json!({
22112213
"stopOnEntry": false,
22122214
"program": directory,
2215+
"cwd": directory,
22132216
"dlvCwd": directory,
22142217
"file": path,
22152218
"root": root,
@@ -2229,6 +2232,61 @@ fn debug_start_arguments_fill_adapter_arguments_from_path_facts() {
22292232
);
22302233
}
22312234

2235+
#[test]
2236+
fn debug_start_arguments_bind_cwd_to_the_directory_of_the_target() {
2237+
let (root, path, directory) = if cfg!(windows) {
2238+
(r"C:\work", r"C:\work\lab\main.go", r"C:\work\lab")
2239+
} else {
2240+
("/work", "/work/lab/main.go", "/work/lab")
2241+
};
2242+
let start = |cwd: &str, target: Value| {
2243+
let mut manifest = fixture("debug-adapter.plugin.json");
2244+
manifest["debugAdapters"][0]["configurations"]["launch"]["targetBindings"] =
2245+
json!({ "cwd": cwd });
2246+
lithe_plugin_manifest::debug_start_arguments_command(json!({
2247+
"manifest": manifest,
2248+
"adapterID": "example",
2249+
"request": "launch",
2250+
"facts": {
2251+
"registration": { "generation": 3, "currentGeneration": 3 },
2252+
"target": target,
2253+
},
2254+
}))
2255+
.expect("payload is well formed")
2256+
};
2257+
let target = json!({ "kind": "file", "path": path, "workingDirectory": root });
2258+
for (cwd, expected) in [
2259+
("target.directory", directory),
2260+
("target.workingDirectory", root),
2261+
] {
2262+
let launch = start(cwd, target.clone());
2263+
assert_eq!(launch["status"], "ok", "{launch}");
2264+
assert_eq!(launch["arguments"]["cwd"], expected, "{cwd}: {launch}");
2265+
}
2266+
2267+
// A bare file name has no directory to run in, and a target without a
2268+
// working directory has none either; both are refused before the start.
2269+
for (cwd, target, field) in [
2270+
(
2271+
"target.directory",
2272+
json!({ "kind": "file", "path": "main.go", "workingDirectory": root }),
2273+
"target.path",
2274+
),
2275+
(
2276+
"target.workingDirectory",
2277+
json!({ "kind": "file", "path": path }),
2278+
"target.workingDirectory",
2279+
),
2280+
] {
2281+
let refused = start(cwd, target);
2282+
assert_eq!(
2283+
refused["error"]["code"], "debug.target.invalid",
2284+
"{refused}"
2285+
);
2286+
assert_eq!(refused["error"]["field"], field, "{refused}");
2287+
}
2288+
}
2289+
22322290
#[test]
22332291
fn debug_start_arguments_add_an_adapter_argument_to_the_environment() {
22342292
let (root, path, directory) = if cfg!(windows) {

‎rust/crates/debug/src/adapters/declaration.rs‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,9 @@ pub struct DebugTargetBindings {
152152
/// Source of the `processId` argument: only `target.processID`. Attach only.
153153
#[serde(default, skip_serializing_if = "Option::is_none")]
154154
pub process_id: Option<String>,
155-
/// Source of the `cwd` argument: only `target.workingDirectory`.
155+
/// Source of the `cwd` argument: `target.workingDirectory`, or
156+
/// `target.directory` (the directory containing `target.path`) for an
157+
/// adapter whose program runs where its package is, such as Delve.
156158
#[serde(default, skip_serializing_if = "Option::is_none")]
157159
pub cwd: Option<String>,
158160
/// Where a remote `host:port` goes. Attach only.

‎rust/crates/debug/src/adapters/plan.rs‎

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -147,15 +147,12 @@ pub fn plan(
147147
if bindings.process_id.is_some() && target.process_id.is_none() {
148148
return Err(missing("target.processID", "process ID"));
149149
}
150-
if bindings.cwd.is_some()
151-
&& target
152-
.working_directory
153-
.as_deref()
154-
.is_none_or(str::is_empty)
150+
// `cwd` and the adapter's own arguments all read path facts.
151+
for fact in bindings
152+
.cwd
153+
.iter()
154+
.chain(bindings.adapter_arguments.iter().flat_map(BTreeMap::values))
155155
{
156-
return Err(missing("target.workingDirectory", "working directory"));
157-
}
158-
for fact in bindings.adapter_arguments.iter().flat_map(BTreeMap::values) {
159156
if path_fact(target, fact).is_some() {
160157
continue;
161158
}

‎rust/crates/debug/src/adapters/start.rs‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,11 @@ fn bound_arguments(
167167
bind(
168168
"cwd",
169169
&bindings.cwd,
170-
target.working_directory.clone().map(Value::from),
170+
bindings
171+
.cwd
172+
.as_deref()
173+
.and_then(|fact| path_fact(target, fact))
174+
.map(Value::from),
171175
);
172176
bind(
173177
"args",
@@ -205,8 +209,8 @@ pub(crate) fn target_directory(path: &str) -> Option<&str> {
205209
.filter(|directory| !directory.is_empty())
206210
}
207211

208-
/// The value of a path fact an `adapterArguments` binding reads, or `None`
209-
/// when the target does not carry it.
212+
/// The value of a path fact a `cwd` or `adapterArguments` binding reads, or
213+
/// `None` when the target does not carry it.
210214
pub(crate) fn path_fact<'a>(target: &'a TargetFacts, fact: &str) -> Option<&'a str> {
211215
let present = |value: &'a Option<String>| value.as_deref().filter(|value| !value.is_empty());
212216
match fact {

‎rust/crates/debug/src/adapters/validate_configuration.rs‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,11 @@ impl DebugConfiguration {
7373
&["target.path", TARGET_DIRECTORY, TARGET_EXECUTABLE][..],
7474
),
7575
("processId", &bindings.process_id, &["target.processID"][..]),
76-
("cwd", &bindings.cwd, &["target.workingDirectory"][..]),
76+
(
77+
"cwd",
78+
&bindings.cwd,
79+
&["target.workingDirectory", TARGET_DIRECTORY][..],
80+
),
7781
("args", &bindings.args, &["target.arguments"][..]),
7882
("env", &bindings.env, &["target.environment"][..]),
7983
] {

‎shared/plugins/contract/v1/README.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2350,8 +2350,8 @@ adapter serves `attach`), and one `onDebug:<id>` activation event per adapter.
23502350
| `requests` | `launch` (default) and/or `attach`. |
23512351
| `selectors[]` | Optional automatic matching by `targetKinds` and `languageIDs`; absent means explicit selection only. |
23522352
| `configurationTypes` | Imported `launch.json` `type` values the adapter answers to. |
2353-
| `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. |
2354-
| `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`. |
2353+
| `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. |
2354+
| `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`. |
23552355
| `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. |
23562356
| `capabilities.disabled` | Standard DAP `supports*` names or `exceptionBreakpointFilters` the host treats as absent. |
23572357
| `features` | Adapter-specific DAP extensions the adapter implements; see [Adapter features and defaults](#adapter-features-and-defaults). |

‎shared/plugins/contract/v1/fixtures/debug-adapters/debug-adapter-rejections.json‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,12 @@
8181
"edits": [{"op": "set", "path": "/block/debugAdapters/0/configurations", "value": {"launch": {"targetBindings": {"program": "target.processID"}}}}],
8282
"expected": {"code": "plugin.manifest.invalid", "field": "debugAdapters[0].configurations.launch.targetBindings.program"}
8383
},
84+
{
85+
"id": "cwd-from-target-path",
86+
"layer": "schema",
87+
"edits": [{"op": "set", "path": "/block/debugAdapters/0/configurations", "value": {"launch": {"targetBindings": {"cwd": "target.path"}}}}],
88+
"expected": {"code": "plugin.manifest.invalid", "field": "debugAdapters[0].configurations.launch.targetBindings.cwd"}
89+
},
8490
{
8591
"id": "function-for-step-back",
8692
"layer": "schema",

‎shared/plugins/contract/v1/plugin-manifest.schema.json‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -698,7 +698,8 @@
698698
"const": "target.processID"
699699
},
700700
"cwd": {
701-
"const": "target.workingDirectory"
701+
"description": "`target.workingDirectory` is the working directory. `target.directory` is the directory containing `target.path`, for adapters whose program runs where its package is.",
702+
"enum": ["target.workingDirectory", "target.directory"]
702703
},
703704
"args": { "const": "target.arguments" },
704705
"env": { "const": "target.environment" },

0 commit comments

Comments
 (0)