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
//! Locating the workspace root and loading its configuration.
use BTreeSet;
use ;
use ;
use FileSystem;
use ;
use crateError;
const MANIFEST: &str = "package.json";
/// The directory the search for a configuration file of `root` stops at: the root of the git repository holding it,
/// and the root itself outside a repository.
pub
/// Whether a directory strictly above `root`, up to and including `ceiling`, holds a `package.json` or a workspace
/// declaration: a standalone package there is nested in another project.
pub
/// The project that contains a start path, and the workspaces above it that could claim it.
pub
/// Locates the project of `start`, climbing no higher than the root of the git repository holding it.
pub
/// The member directories of the workspace at `root`, discovered with its own configuration.
///
/// The caller picks the policy for a root that cannot be configured or discovered: [`find_workspace_root`] falls back
/// to the declared patterns, [`find_target`](crate::find_target) reports the error when the root lists the project.
pub
/// Finds the workspace that `start` belongs to.
///
/// The project of `start` is its nearest ancestor (inclusive) with a `package.json` or a workspace declaration. When
/// the project declares a workspace itself, it is the root. Otherwise the nearest ancestor workspace that lists the
/// project as a member (after negations) is the root, and when none does, the project is its own root as a single
/// package: an ancestor workspace never claims a project it does not include.
///
/// A workspace that declares itself is its own root even when an outer workspace lists it as a member, as it is for
/// [`discover_all`](crate::discover_all). Ancestor workspaces are read with their own configuration, so their
/// package-manager choice decides which patterns count.
///
/// The search never climbs above the root of the git repository holding `start` (the nearest directory with a
/// `.git`), so a stray `package.json` in a parent of the repository is never taken for its workspace.
///
/// `start` may be a file or a directory and must be rooted (absolute); the search never consults the working
/// directory, so a relative path is rejected rather than silently searching only its own lexical ancestors.
///
/// # Errors
/// [`Error::RelativePath`] when `start` is relative, [`Error::NoWorkspace`] when `start` and its ancestors hold no
/// `package.json` and declare no workspace.
///
/// # Examples
/// ```
/// use std::path::Path;
/// use skif::{MemoryFs, find_workspace_root};
///
/// let fs = MemoryFs::new()
/// .with_file("/repo/package.json", r#"{"workspaces": ["packages/*"]}"#)
/// .with_file("/repo/packages/a/package.json", r#"{"name": "a"}"#)
/// .with_file("/repo/other/package.json", r#"{"name": "other"}"#);
/// assert_eq!(find_workspace_root(Path::new("/repo/packages/a"), &fs)?, Path::new("/repo"));
/// assert_eq!(find_workspace_root(Path::new("/repo/other"), &fs)?, Path::new("/repo/other"));
/// # Ok::<(), skif::Error>(())
/// ```
/// The name of the configuration file in `root` (`skif.toml` or `.skif.toml`), relative to `root`, when there is one.
///
/// Pass it to [`RunOptions::with_config_path`](crate::RunOptions::with_config_path) so diagnostics about the
/// configuration point at the right file. When both files exist [`load_config`] fails, and this returns the first.
///
/// # Examples
/// ```
/// use std::path::Path;
/// use skif::{MemoryFs, config_file};
///
/// let fs = MemoryFs::new().with_file("/repo/.skif.toml", "");
/// assert_eq!(config_file(Path::new("/repo"), &fs), Some(".skif.toml"));
/// ```
/// Resolves the configuration of `root` through `fs`: every `skif.toml` or `.skif.toml` from `root` up to `ceiling`
/// (stopping after a file with `root = true`), with what they `extend`, merged farthest first, then `overrides` on top
/// (defaults < files < overrides). Returns the merged configuration with the files it came from and where each key
/// was set.
///
/// `cache` remembers which directories hold a configuration file and every file parsed, so pass one cache to all
/// the roots of a run, in parallel if you like: each file is read once.
///
/// # Errors
/// [`ConfigError::Ambiguous`] when a directory of the chain has both files, [`ConfigError::Io`] when a file cannot be
/// read, [`ConfigError::Parse`], [`ConfigError::Invalid`] and [`ConfigError::Scope`] (carrying the file, line and
/// column) for bad contents, [`ConfigError::ExtendsCycle`] for files that extend each other, and
/// [`ConfigError::Invalid`] for an override that is not a valid selector.
/// Loads the configuration of `root` through `fs` and layers `overrides` over it (defaults < files < overrides).
///
/// The files are `root`'s own `skif.toml` or `.skif.toml` and those of its parent directories up to the root of the
/// git repository (`root` alone outside one), stopping after a file with `root = true`; each file also brings in the
/// files it `extends`. No file means the default configuration. See [`resolve_config`] for the other ceilings and
/// for where each setting came from.
///
/// # Errors
/// [`ConfigError::Ambiguous`] when both files exist in a directory, [`ConfigError::Io`] when one cannot be read,
/// [`ConfigError::Parse`] / [`ConfigError::Invalid`] (carrying the file, line and column) for bad contents, and
/// [`ConfigError::Invalid`] for an override that is not a valid selector.
///
/// # Examples
/// ```
/// use std::path::Path;
/// use skif::{ConfigOverrides, MemoryFs, load_config};
///
/// let fs = MemoryFs::new().with_file("/repo/skif.toml", "[lint]\nfail-on-warnings = true\n");
/// let config = load_config(Path::new("/repo"), &fs, &ConfigOverrides::default())?;
/// assert!(config.lint.fail_on_warnings);
/// # Ok::<(), skif::ConfigError>(())
/// ```