Skip to content

Commit 26db6a4

Browse files
a workspace's lock entry is keyed the same way whatever members a command plans (#868)
Two members spelling one dependency two ways (`tinyhttps.workspace = true` and `[dependencies.mcpplibs] tinyhttps.workspace = true`) made `-p` of each write its own key and hash. The key is now the workspace's spelling: its `[dependencies]`, `[workspace.dependencies]`, then every member in the order `members` lists them, selected or not (docs/07, e2e 990). Closes #868 Co-authored-by: speak-agent <248744407+speak-agent@users.noreply.github.com>
1 parent 0bca47a commit 26db6a4

5 files changed

Lines changed: 169 additions & 11 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,15 @@
2020
runs mcpp for a project: in its root, by the name on `PATH` or a set path,
2121
with `XLINGS_PROJECT_DIR` removed (design 2026-10-30 part 2 §4.0).
2222

23+
### Fixed
24+
25+
- **A workspace's lock entry is keyed the same way whatever members a command
26+
plans** (#868). Two members spelling one dependency two ways
27+
(`tinyhttps.workspace = true`, `[dependencies.mcpplibs] tinyhttps.workspace =
28+
true`) made `-p` of each write its own key and hash. The key is the
29+
workspace's spelling: its `[dependencies]`, `[workspace.dependencies]`, then
30+
the members in the order `members` lists them (docs/07, e2e 990).
31+
2332
## [2026.10.30.1] - 2026-10-30
2433

2534
### Added

‎docs/07-workspace.md‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -712,7 +712,11 @@ myproject/
712712
one, covers every member that has been built or configured.
713713
- `mcpp.lock` at the workspace root records the resolution of every member.
714714
`mcpp build --workspace` writes the whole record; `mcpp build -p X` updates
715-
the entries of X's graph.
715+
the entries of X's graph. An entry's key is the spelling under which the
716+
workspace declares the dependency: its own `[dependencies]`, then
717+
`[workspace.dependencies]`, then each member in the order `members` lists
718+
them, whether the command selects it or not. The key and its hash therefore
719+
do not depend on which members a command plans.
716720
- A member's build program writes to `<member>/target/.build-mcpp/`. The
717721
members' build programs are compiled in the workspace's build programs'
718722
graph, `target/.build-mcpp/host/`, and a library several of them import is

‎docs/zh/07-workspace.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -611,7 +611,8 @@ myproject/
611611
- 放入多个产物目录的共享库,在支持硬链接的文件系统上是同一个文件的多个名字,其他情况下被复制。
612612
- 工作空间根的编译数据库(在 `[build] compile_commands` 指定时)覆盖所有已构建或已配置的成员。
613613
- 工作空间根的 `mcpp.lock` 记录全体成员的解析结果。`mcpp build --workspace` 写入完整记录;
614-
`mcpp build -p X` 更新 X 所在图中的条目。
614+
`mcpp build -p X` 更新 X 所在图中的条目。条目的键取工作空间声明该依赖时的写法:先看自身的 `[dependencies]`,再看
615+
`[workspace.dependencies]`,再按 `members` 列出的顺序看各成员,无论命令是否选中它。键与其散列因此不随命令规划哪些成员而变。
615616
- 成员的构建程序写入 `<member>/target/.build-mcpp/`。各成员的构建程序在 workspace 的构建程序图
616617
`target/.build-mcpp/host/` 中编译,多个程序 import 的库只编一次。
617618
- 早期版本的 mcpp 在成员自己的 `target/` 下留下的构建目录不再被读取;`mcpp clean --stale`

‎src/build/prepare/records.cpp‎

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import mcpp.runtime.search;
1616
import mcpp.libs.json;
1717
import mcpp.log;
1818
import mcpp.manifest;
19+
import mcpp.project; // load_member_manifest: every member's spelling of a dependency (#868)
1920
import mcpp.source_kind;
2021
import mcpp.toolchain.clang;
2122
import mcpp.toolchain.hostflags; // the compile-token producer the package std module reuses
@@ -132,22 +133,41 @@ std::expected<void, std::string> step13_lockfile(PrepareState& state, BuildConte
132133
// the spelling that existing committed locks hold, and it is the one
133134
// #329 already fixes for git entries, so it stays; the merge and the
134135
// `--locked` comparison work on the identity, not on the spelling.
136+
//
137+
// THE SPELLING DOES NOT DEPEND ON WHICH MEMBERS A COMMAND PLANS (#868).
138+
// Reading the selected members made `mcpp build -p pack` write
139+
// `tinyhttps` and `mcpp build -p model-gateway` `mcpplibs.tinyhttps`
140+
// (and a different hash) from one tree: two members spelled it two
141+
// ways. Where the project is a workspace (its lock is the workspace's)
142+
// the manifests read are therefore the workspace's own, `[dependencies]`
143+
// then `[workspace.dependencies]`, and then every member in the order
144+
// `[workspace] members` lists them, selected or not; the first that
145+
// declares the dependency gives its spelling.
146+
std::vector<const mcpp::manifest::Manifest*> declaring;
147+
std::vector<mcpp::manifest::Manifest> memberManifests;
148+
if (state.wsManifest && !state.runtimeWorkspaceRoot.empty()) {
149+
memberManifests.reserve(state.wsManifest->workspace.members.size());
150+
for (auto const& mp : state.wsManifest->workspace.members)
151+
if (auto mm = mcpp::project::load_member_manifest(
152+
*state.wsManifest, state.runtimeWorkspaceRoot, mp))
153+
memberManifests.push_back(std::move(*mm));
154+
declaring.push_back(&*state.wsManifest);
155+
for (auto const& mm : memberManifests) declaring.push_back(&mm);
156+
} else {
157+
declaring.push_back(&*state.m);
158+
}
135159
auto lock_name_for = [&](const ResolvedKey& k) -> std::string {
136-
auto declared = [&](const mcpp::manifest::Manifest& mf)
160+
auto declared_in = [&](const std::map<std::string, mcpp::manifest::DependencySpec>& deps)
137161
-> std::optional<std::string> {
138-
for (auto const& [n, s] : mf.dependencies) {
162+
for (auto const& [n, s] : deps) {
139163
const std::string sn = s.shortName.empty() ? n : s.shortName;
140164
if (s.namespace_ == k.ns && sn == k.shortName) return n;
141165
}
142166
return std::nullopt;
143167
};
144-
if (auto n = declared(*state.m)) return *n;
145-
if (state.workspacePlan()) {
146-
if (state.wsManifest)
147-
if (auto n = declared(*state.wsManifest)) return *n;
148-
for (std::size_t i = 1; i < state.packages.size(); ++i)
149-
if (state.packages[i].selectedMember)
150-
if (auto n = declared(state.packages[i].manifest)) return *n;
168+
for (const auto* mf : declaring) {
169+
if (auto n = declared_in(mf->dependencies)) return *n;
170+
if (auto n = declared_in(mf->workspace.dependencies)) return *n;
151171
}
152172
return mcpp::pm::compat::qualified_name(k.ns, k.shortName);
153173
};
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
#!/usr/bin/env bash
2+
# requires: gcc fresh-sandbox
3+
# 990 -- a lock entry's key and hash do not depend on which members a command
4+
# plans (#868).
5+
#
6+
# Two members take one workspace dependency under two spellings: `a` writes
7+
# `widget2.workspace = true`, `c` writes `[dependencies.mcpplibs]
8+
# widget2.workspace = true`. The key was the spelling of a selected member, so
9+
# `-p a` wrote `[package."widget2"]` and `-p c` `[package."mcpplibs.widget2"]`,
10+
# each with its own hash (the hash is taken over the key), and the lock changed
11+
# between commands with no change to a manifest.
12+
#
13+
# Criteria: `--workspace`, `-p a` and `-p c` write the same key and hash; it is
14+
# the workspace's own spelling (`[workspace.dependencies] widget2`), which
15+
# precedes every member's.
16+
set -e
17+
18+
TMP=$(mktemp -d)
19+
trap "rm -rf $TMP" EXIT
20+
21+
export MCPP_HOME="$TMP/mcpp-home"
22+
source "$(dirname "$0")/_inherit_toolchain.sh"
23+
24+
mkdir -p "$TMP/ws"
25+
cd "$TMP/ws"
26+
27+
# A package of the default namespace, from a project-local index, with its
28+
# payload already in place: nothing is fetched.
29+
mkdir -p local-index/pkgs/w .mcpp/.xlings/data/xpkgs/mcpplibs.widget2/1.0.0/src
30+
cat > local-index/pkgs/w/widget2.lua <<'EOF'
31+
package = {
32+
spec = "1",
33+
namespace = "mcpplibs",
34+
name = "widget2",
35+
description = "a package of the default namespace from a local index",
36+
licenses = {"MIT"},
37+
type = "package",
38+
xpm = {
39+
linux = { ["1.0.0"] = { url = "https://example.invalid/w.tar.gz" } },
40+
macosx = { ["1.0.0"] = { url = "https://example.invalid/w.tar.gz" } },
41+
windows = { ["1.0.0"] = { url = "https://example.invalid/w.zip" } },
42+
},
43+
mcpp = {
44+
language = "c++23",
45+
import_std = false,
46+
sources = { "src/widget2.cppm" },
47+
targets = { ["widget2"] = { kind = "lib" } },
48+
deps = {},
49+
},
50+
}
51+
EOF
52+
printf 'export module widget2;\nexport int widget2_value() { return 35; }\n' \
53+
> .mcpp/.xlings/data/xpkgs/mcpplibs.widget2/1.0.0/src/widget2.cppm
54+
55+
cat > mcpp.toml <<'EOF'
56+
[workspace]
57+
members = ["a", "c"]
58+
59+
[workspace.indices]
60+
mcpplibs = { path = "local-index" }
61+
62+
[workspace.dependencies]
63+
widget2 = "1.0.0"
64+
EOF
65+
66+
mkdir -p a/src c/src
67+
cat > a/mcpp.toml <<'EOF'
68+
[package]
69+
name = "a"
70+
version = "0.1.0"
71+
72+
[dependencies]
73+
widget2.workspace = true
74+
75+
[targets.a]
76+
kind = "lib"
77+
EOF
78+
printf 'export module a;\nimport widget2;\nexport int a_value() { return widget2_value() + 7; }\n' > a/src/a.cppm
79+
80+
cat > c/mcpp.toml <<'EOF'
81+
[package]
82+
name = "c"
83+
version = "0.1.0"
84+
85+
[dependencies.mcpplibs]
86+
widget2.workspace = true
87+
88+
[targets.c]
89+
kind = "bin"
90+
main = "src/main.cpp"
91+
EOF
92+
printf 'import widget2;\nint main() { return widget2_value() == 35 ? 0 : 1; }\n' > c/src/main.cpp
93+
94+
entry() { # the lock's widget2 entry: its header and its hash
95+
grep -E '^\[package\..*widget2"\]' mcpp.lock
96+
awk '/widget2"\]/{f=1} f && /^hash/{print; exit}' mcpp.lock
97+
}
98+
99+
declare -A seen
100+
for sel in "--workspace" "-p a" "-p c"; do
101+
rm -f mcpp.lock
102+
# shellcheck disable=SC2086
103+
"$MCPP" build $sel --configure-only > "build.log" 2>&1 || {
104+
cat build.log
105+
echo "FAIL: mcpp build $sel --configure-only"
106+
exit 1
107+
}
108+
seen[$sel]="$(entry)"
109+
done
110+
111+
for sel in "--workspace" "-p a" "-p c"; do
112+
[[ "${seen[$sel]}" == "${seen[--workspace]}" ]] || {
113+
for s in "--workspace" "-p a" "-p c"; do echo "$s: ${seen[$s]}"; done
114+
echo "FAIL: the lock entry of widget2 depends on the members a command plans"
115+
exit 1
116+
}
117+
done
118+
grep -q '^\[package\."widget2"\]' <<<"${seen[--workspace]}" || {
119+
echo "${seen[--workspace]}"
120+
echo "FAIL: the key is not the workspace's own spelling (widget2)"
121+
exit 1
122+
}
123+
124+
echo "OK"

0 commit comments

Comments
 (0)