1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
//! Go package resolution.
//!
//! A Go import names a package, and a package is a directory: every
//! `.go` file in it is part of the same package and sees the others with
//! no import statement. Mapping an import path onto a directory needs
//! the module path each `go.mod` declares, which is the one piece of
//! this that has to be read from a file rather than derived from the
//! workspace layout.
use std::path::Path;
use serde::{Deserialize, Serialize};
/// The `module` lines of every `go.mod` in the workspace, longest module
/// path first so a nested module wins over the one containing it.
///
/// This is the revision of the manifest half of module resolution: two
/// equal values resolve every Go import identically, so a manifest edit
/// that leaves the declarations alone (a `require` bump, a comment)
/// invalidates nothing. The snapshot stores it so a restore resolves
/// against the same manifests its saved call graph was linked under.
#[derive(Debug, Default, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub(crate) struct GoModules {
/// `(declared module path, workspace-relative directory of its go.mod)`.
modules: Vec<(String, String)>,
}
impl GoModules {
/// Build from `(workspace-relative go.mod path, its contents)` pairs.
pub(crate) fn from_manifests<'a, I>(manifests: I) -> Self
where
I: IntoIterator<Item = (&'a str, &'a str)>,
{
let mut modules: Vec<(String, String)> = Vec::new();
for (path, contents) in manifests {
let Some(module_path) = declared_module_path(contents) else {
continue;
};
let dir = path
.rsplit_once('/')
.map(|(head, _)| head.to_string())
.unwrap_or_default();
modules.push((module_path, dir));
}
// A nested module declares a longer path than its parent, and an
// import matching both belongs to the nested one.
modules.sort_by(|a, b| b.0.len().cmp(&a.0.len()).then_with(|| a.0.cmp(&b.0)));
GoModules { modules }
}
/// Read the workspace-relative manifests at `paths` under `root`. A
/// path that no longer exists or no longer declares a module simply
/// contributes nothing, which is how a removed manifest is forgotten.
pub(crate) fn read<I>(root: &Path, paths: I) -> Self
where
I: IntoIterator<Item = String>,
{
let mut manifests: Vec<(String, String)> = paths
.into_iter()
.filter_map(|rel| {
std::fs::read_to_string(root.join(&rel))
.ok()
.map(|contents| (rel, contents))
})
.collect();
manifests.sort();
manifests.dedup_by(|a, b| a.0 == b.0);
Self::from_manifests(
manifests
.iter()
.map(|(path, contents)| (path.as_str(), contents.as_str())),
)
}
/// Workspace-relative paths of the manifests that declared a module.
pub(crate) fn manifest_paths(&self) -> impl Iterator<Item = String> + '_ {
self.modules.iter().map(|(_, dir)| {
if dir.is_empty() {
GO_MANIFEST.to_string()
} else {
format!("{dir}/{GO_MANIFEST}")
}
})
}
/// The workspace-relative directory an import path names, or `None`
/// when the import belongs to no module declared in this workspace
/// (the standard library and external dependencies).
pub(crate) fn directory_for(&self, import_path: &str) -> Option<String> {
for (module_path, dir) in &self.modules {
if import_path == module_path {
return Some(dir.clone());
}
if let Some(rest) = import_path.strip_prefix(module_path) {
if let Some(rest) = rest.strip_prefix('/') {
return Some(if dir.is_empty() {
rest.to_string()
} else {
format!("{dir}/{rest}")
});
}
}
}
None
}
/// Whether any module was declared. An empty index means every Go
/// import resolves to nothing for want of a manifest, which is a
/// different answer from an import that names an external package.
#[cfg(test)]
pub(crate) fn is_empty(&self) -> bool {
self.modules.is_empty()
}
}
/// The manifest file name. The workspace walk reports every file with this
/// name, wherever it sits, so a nested module is found the same way.
pub(crate) const GO_MANIFEST: &str = "go.mod";
/// The path on `go.mod`'s `module` line.
fn declared_module_path(contents: &str) -> Option<String> {
for line in contents.lines() {
let trimmed = line.trim();
if let Some(rest) = trimmed.strip_prefix("module ") {
let path = rest.trim().trim_matches('"');
if !path.is_empty() {
return Some(path.to_string());
}
}
}
None
}
/// Extract the import paths from Go source.
///
/// Go writes almost every import inside a parenthesised block, so the
/// line-oriented keyword matcher used for other languages sees only the
/// `import (` opener and never a single path. Both the block form and
/// the single-line form are handled here, along with the alias, blank
/// and dot spellings (`m "x"`, `_ "x"`, `. "x"`).
pub(crate) fn extract_go_imports(source: &str) -> Vec<String> {
let mut out: Vec<String> = Vec::new();
let mut in_block = false;
for line in source.lines() {
let trimmed = line.trim();
if in_block {
if trimmed.starts_with(')') {
in_block = false;
continue;
}
if let Some(path) = quoted_path(trimmed) {
out.push(path);
}
continue;
}
if trimmed == "import (" || trimmed.starts_with("import (") {
in_block = true;
continue;
}
if let Some(rest) = trimmed.strip_prefix("import ") {
if let Some(path) = quoted_path(rest) {
out.push(path);
}
}
}
out
}
/// The contents of the first double-quoted run in `text`, ignoring any
/// alias token before it and any comment after it.
fn quoted_path(text: &str) -> Option<String> {
let (_, rest) = text.split_once('"')?;
let (path, _) = rest.split_once('"')?;
if path.is_empty() {
return None;
}
Some(path.to_string())
}
#[cfg(test)]
mod tests {
use super::*;
const MANIFEST: &str = "module github.com/acme/tool\n\ngo 1.26.1\n";
#[test]
fn block_imports_are_extracted_one_path_per_entry() {
let source = "package runtime\n\nimport (\n\t\"fmt\"\n\n\t\"github.com/acme/tool/internal/store\"\n)\n\nfunc F() {}\n";
assert_eq!(
extract_go_imports(source),
vec![
"fmt".to_string(),
"github.com/acme/tool/internal/store".to_string()
]
);
}
#[test]
fn alias_blank_and_dot_imports_all_yield_their_path() {
let source = "import (\n\tm \"github.com/acme/tool/a\"\n\t_ \"github.com/acme/tool/b\"\n\t. \"github.com/acme/tool/c\"\n)\n";
assert_eq!(
extract_go_imports(source),
vec![
"github.com/acme/tool/a".to_string(),
"github.com/acme/tool/b".to_string(),
"github.com/acme/tool/c".to_string()
]
);
}
#[test]
fn single_line_import_is_extracted_too() {
assert_eq!(extract_go_imports("import \"fmt\"\n"), vec!["fmt"]);
}
#[test]
fn code_after_the_block_is_not_mistaken_for_an_import() {
let source = "import (\n\t\"fmt\"\n)\n\nvar s = \"github.com/acme/tool/nope\"\n";
assert_eq!(extract_go_imports(source), vec!["fmt"]);
}
#[test]
fn import_path_maps_onto_the_declaring_modules_directory() {
let mods = GoModules::from_manifests([("go.mod", MANIFEST)]);
assert_eq!(
mods.directory_for("github.com/acme/tool/internal/store")
.as_deref(),
Some("internal/store")
);
assert_eq!(
mods.directory_for("github.com/acme/tool").as_deref(),
Some("")
);
}
#[test]
fn a_module_in_a_subdirectory_anchors_on_that_subdirectory() {
let mods = GoModules::from_manifests([("sdks/go/go.mod", MANIFEST)]);
assert_eq!(
mods.directory_for("github.com/acme/tool/client").as_deref(),
Some("sdks/go/client")
);
}
#[test]
fn a_nested_module_wins_over_the_one_containing_it() {
let mods = GoModules::from_manifests([
("go.mod", "module github.com/acme/tool\n"),
("plugin/go.mod", "module github.com/acme/tool/plugin\n"),
]);
// Both prefixes match; the longer declaration owns the path.
assert_eq!(
mods.directory_for("github.com/acme/tool/plugin/x")
.as_deref(),
Some("plugin/x")
);
assert_eq!(
mods.directory_for("github.com/acme/tool/internal")
.as_deref(),
Some("internal")
);
}
#[test]
fn an_external_or_stdlib_import_names_no_workspace_directory() {
let mods = GoModules::from_manifests([("go.mod", MANIFEST)]);
assert_eq!(mods.directory_for("fmt"), None);
assert_eq!(mods.directory_for("github.com/other/dep"), None);
// A prefix that is not a path boundary must not match.
assert_eq!(mods.directory_for("github.com/acme/toolkit/x"), None);
}
#[test]
fn a_workspace_with_no_manifest_declares_no_modules() {
let mods = GoModules::from_manifests([("go.mod", "go 1.26.1\n")]);
assert!(mods.is_empty());
assert_eq!(mods.directory_for("github.com/acme/tool/x"), None);
}
}