Skip to content

Commit b7ce5e2

Browse files
authored
Read bs-dev-dependencies if --dev was passed. (#7650)
* Read bs-dev-dependencies if --dev was passed. * Try and check the file type when allowing dev dependency access. * Store is_type_dev in SourceFileMeta. * Only allow usage of dev_dependencies when type:dev is true for source file * Traverse rescript config for compiler-args command to find out if type = dev * Collect all qualified package sources * Expand search for type:dev in rescript config. * Add some unit test to cover find_is_type_dev_for_path * Add is_type_dev to Module as well. * Remove pub * Add changelog entry * Add negative test * Add snapshot * Normalize paths * Try --ignore-cr-at-eol * Revert "Try --ignore-cr-at-eol" This reverts commit 3a67a8f. * Trigger CI * Remove extra lines in changelog * Add tmate on Windows * Add tmate on Windows but earlier 🙃 * Remove more stuff * More general path normalize check on Windows * This has to be newlines * Update to beta 1 * Revert line changes
1 parent cea6328 commit b7ce5e2

File tree

16 files changed

+313
-62
lines changed

16 files changed

+313
-62
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
- Fix inside comment printing for empty dict. https://github.com/rescript-lang/rescript/pull/7654
4545
- Fix I/O error message when trying to extract extra info from non-existing file. https://github.com/rescript-lang/rescript/pull/7656
4646
- Fix fatal error when JSX expression used without configuring JSX in rescript.json. https://github.com/rescript-lang/rescript/pull/7656
47+
- Rewatch: Only allow access to `"bs-dev-dependencies"` from `"type": "dev"` source files. https://github.com/rescript-lang/rescript/pull/7650
4748

4849
# 12.0.0-beta.1
4950

rewatch/src/build.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ pub mod packages;
88
pub mod parse;
99
pub mod read_compile_state;
1010

11+
use self::compile::compiler_args;
12+
use self::parse::parser_args;
1113
use crate::build::compile::{mark_modules_with_deleted_deps_dirty, mark_modules_with_expired_deps_dirty};
1214
use crate::helpers::emojis::*;
1315
use crate::helpers::{self, get_workspace_root};
@@ -26,9 +28,6 @@ use std::path::{Path, PathBuf};
2628
use std::process::Stdio;
2729
use std::time::{Duration, Instant};
2830

29-
use self::compile::compiler_args;
30-
use self::parse::parser_args;
31-
3231
fn is_dirty(module: &Module) -> bool {
3332
match module.source_type {
3433
SourceType::SourceFile(SourceFile {
@@ -56,14 +55,18 @@ pub struct CompilerArgs {
5655
pub parser_args: Vec<String>,
5756
}
5857

59-
pub fn get_compiler_args(path: &Path, build_dev_deps: bool) -> Result<String> {
58+
pub fn get_compiler_args(path: &Path) -> Result<String> {
6059
let filename = &helpers::get_abs_path(path);
6160
let package_root =
6261
helpers::get_abs_path(&helpers::get_nearest_config(&path).expect("Couldn't find package root"));
6362
let workspace_root = get_workspace_root(&package_root).map(|p| helpers::get_abs_path(&p));
6463
let root_rescript_config =
6564
packages::read_config(&workspace_root.to_owned().unwrap_or(package_root.to_owned()))?;
6665
let rescript_config = packages::read_config(&package_root)?;
66+
let is_type_dev = match filename.strip_prefix(&package_root) {
67+
Err(_) => false,
68+
Ok(relative_path) => root_rescript_config.find_is_type_dev_for_path(relative_path),
69+
};
6770

6871
// make PathBuf from package root and get the relative path for filename
6972
let relative_filename = filename.strip_prefix(PathBuf::from(&package_root)).unwrap();
@@ -97,7 +100,7 @@ pub fn get_compiler_args(path: &Path, build_dev_deps: bool) -> Result<String> {
97100
&package_root,
98101
&workspace_root,
99102
&None,
100-
build_dev_deps,
103+
is_type_dev,
101104
true,
102105
);
103106

@@ -281,7 +284,6 @@ pub fn incremental_build(
281284
show_progress: bool,
282285
only_incremental: bool,
283286
create_sourcedirs: bool,
284-
build_dev_deps: bool,
285287
snapshot_output: bool,
286288
) -> Result<(), IncrementalBuildError> {
287289
logs::initialize(&build_state.packages);
@@ -393,7 +395,6 @@ pub fn incremental_build(
393395
show_progress,
394396
|| pb.inc(1),
395397
|size| pb.set_length(size),
396-
build_dev_deps,
397398
)
398399
.map_err(|e| IncrementalBuildError {
399400
kind: IncrementalBuildErrorKind::CompileError(Some(e.to_string())),
@@ -500,7 +501,6 @@ pub fn build(
500501
show_progress,
501502
false,
502503
create_sourcedirs,
503-
build_dev_deps,
504504
snapshot_output,
505505
) {
506506
Ok(_) => {

rewatch/src/build/build_types.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ pub struct Module {
7171
pub last_compiled_cmi: Option<SystemTime>,
7272
pub last_compiled_cmt: Option<SystemTime>,
7373
pub deps_dirty: bool,
74+
pub is_type_dev: bool,
7475
}
7576

7677
impl Module {

rewatch/src/build/compile.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ pub fn compile(
2222
show_progress: bool,
2323
inc: impl Fn() + std::marker::Sync,
2424
set_length: impl Fn(u64),
25-
build_dev_deps: bool,
2625
) -> anyhow::Result<(String, String, usize)> {
2726
let mut compiled_modules = AHashSet::<String>::new();
2827
let dirty_modules = build_state
@@ -170,7 +169,6 @@ pub fn compile(
170169
&build_state.packages,
171170
&build_state.project_root,
172171
&build_state.workspace_root,
173-
build_dev_deps,
174172
);
175173
Some(result)
176174
}
@@ -186,7 +184,6 @@ pub fn compile(
186184
&build_state.packages,
187185
&build_state.project_root,
188186
&build_state.workspace_root,
189-
build_dev_deps,
190187
);
191188
let cmi_digest_after = helpers::compute_file_hash(Path::new(&cmi_path));
192189

@@ -360,13 +357,13 @@ pub fn compiler_args(
360357
// if packages are known, we pass a reference here
361358
// this saves us a scan to find their paths
362359
packages: &Option<&AHashMap<String, packages::Package>>,
363-
build_dev_deps: bool,
360+
// Is the file listed as "type":"dev"?
361+
is_type_dev: bool,
364362
is_local_dep: bool,
365363
) -> Vec<String> {
366364
let bsc_flags = config::flatten_flags(&config.bsc_flags);
367365

368-
let dependency_paths =
369-
get_dependency_paths(config, project_root, workspace_root, packages, build_dev_deps);
366+
let dependency_paths = get_dependency_paths(config, project_root, workspace_root, packages, is_type_dev);
370367

371368
let module_name = helpers::file_path_to_module_name(file_path, &config.get_namespace());
372369

@@ -504,7 +501,7 @@ fn get_dependency_paths(
504501
project_root: &Path,
505502
workspace_root: &Option<PathBuf>,
506503
packages: &Option<&AHashMap<String, packages::Package>>,
507-
build_dev_deps: bool,
504+
is_file_type_dev: bool,
508505
) -> Vec<Vec<String>> {
509506
let normal_deps = config
510507
.bs_dependencies
@@ -513,7 +510,9 @@ fn get_dependency_paths(
513510
.into_iter()
514511
.map(DependentPackage::Normal)
515512
.collect();
516-
let dev_deps = if build_dev_deps {
513+
514+
// We can only access dev dependencies for source_files that are marked as "type":"dev"
515+
let dev_deps = if is_file_type_dev {
517516
config
518517
.bs_dev_dependencies
519518
.clone()
@@ -569,7 +568,6 @@ fn compile_file(
569568
packages: &AHashMap<String, packages::Package>,
570569
project_root: &Path,
571570
workspace_root: &Option<PathBuf>,
572-
build_dev_deps: bool,
573571
) -> Result<Option<String>, String> {
574572
let ocaml_build_path_abs = package.get_ocaml_build_path();
575573
let build_path_abs = package.get_build_path();
@@ -583,6 +581,7 @@ fn compile_file(
583581
}?;
584582
let module_name = helpers::file_path_to_module_name(implementation_file_path, &package.namespace);
585583
let has_interface = module.get_interface().is_some();
584+
let is_type_dev = module.is_type_dev;
586585
let to_mjs_args = compiler_args(
587586
&package.config,
588587
&root_package.config,
@@ -593,7 +592,7 @@ fn compile_file(
593592
project_root,
594593
workspace_root,
595594
&Some(packages),
596-
build_dev_deps,
595+
is_type_dev,
597596
package.is_local_dep,
598597
);
599598

rewatch/src/build/packages.rs

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use std::time::SystemTime;
1919
#[derive(Debug, Clone)]
2020
pub struct SourceFileMeta {
2121
pub modified: SystemTime,
22+
pub is_type_dev: bool,
2223
}
2324

2425
#[derive(Debug, Clone)]
@@ -112,6 +113,13 @@ impl Package {
112113
.expect("namespace should be set for mlmap module");
113114
self.get_build_path().join(format!("{}.cmi", suffix))
114115
}
116+
117+
pub fn is_source_file_type_dev(&self, path: &Path) -> bool {
118+
self.source_files
119+
.as_ref()
120+
.and_then(|sf| sf.get(path).map(|sfm| sfm.is_type_dev))
121+
.unwrap_or(false)
122+
}
115123
}
116124

117125
impl PartialEq for Package {
@@ -138,6 +146,7 @@ pub fn read_folders(
138146
package_dir: &Path,
139147
path: &Path,
140148
recurse: bool,
149+
is_type_dev: bool,
141150
) -> Result<AHashMap<PathBuf, SourceFileMeta>, Box<dyn error::Error>> {
142151
let mut map: AHashMap<PathBuf, SourceFileMeta> = AHashMap::new();
143152
let path_buf = PathBuf::from(path);
@@ -147,6 +156,7 @@ pub fn read_folders(
147156
path.to_owned(),
148157
SourceFileMeta {
149158
modified: meta.modified().unwrap(),
159+
is_type_dev,
150160
},
151161
)
152162
});
@@ -159,7 +169,7 @@ pub fn read_folders(
159169
let path_ext = entry_path_buf.extension().and_then(|x| x.to_str());
160170
let new_path = path_buf.join(&name);
161171
if metadata.file_type().is_dir() && recurse {
162-
match read_folders(filter, package_dir, &new_path, recurse) {
172+
match read_folders(filter, package_dir, &new_path, recurse, is_type_dev) {
163173
Ok(s) => map.extend(s),
164174
Err(e) => log::error!("Could not read directory: {}", e),
165175
}
@@ -174,6 +184,7 @@ pub fn read_folders(
174184
path,
175185
SourceFileMeta {
176186
modified: metadata.modified().unwrap(),
187+
is_type_dev,
177188
},
178189
);
179190
}
@@ -297,11 +308,16 @@ fn read_dependencies(
297308
project_root: &Path,
298309
workspace_root: &Option<PathBuf>,
299310
show_progress: bool,
311+
build_dev_deps: bool,
300312
) -> Vec<Dependency> {
301-
return parent_config
302-
.bs_dependencies
303-
.to_owned()
304-
.unwrap_or_default()
313+
let mut dependencies = parent_config.bs_dependencies.to_owned().unwrap_or_default();
314+
315+
// Concatenate dev dependencies if build_dev_deps is true
316+
if build_dev_deps && let Some(dev_deps) = parent_config.bs_dev_dependencies.to_owned() {
317+
dependencies.extend(dev_deps);
318+
}
319+
320+
dependencies
305321
.iter()
306322
.filter_map(|package_name| {
307323
if registered_dependencies_set.contains(package_name) {
@@ -360,7 +376,8 @@ fn read_dependencies(
360376
&canonical_path,
361377
project_root,
362378
workspace_root,
363-
show_progress
379+
show_progress,
380+
build_dev_deps
364381
);
365382

366383
Dependency {
@@ -371,7 +388,7 @@ fn read_dependencies(
371388
dependencies,
372389
}
373390
})
374-
.collect::<Vec<Dependency>>();
391+
.collect()
375392
}
376393

377394
fn flatten_dependencies(dependencies: Vec<Dependency>) -> Vec<Dependency> {
@@ -461,6 +478,7 @@ fn read_packages(
461478
project_root: &Path,
462479
workspace_root: &Option<PathBuf>,
463480
show_progress: bool,
481+
build_dev_deps: bool,
464482
) -> Result<AHashMap<String, Package>> {
465483
let root_config = read_config(project_root)?;
466484

@@ -477,6 +495,7 @@ fn read_packages(
477495
project_root,
478496
workspace_root,
479497
show_progress,
498+
build_dev_deps,
480499
));
481500
dependencies.iter().for_each(|d| {
482501
if !map.contains_key(&d.name) {
@@ -515,9 +534,14 @@ pub fn get_source_files(
515534
};
516535

517536
let path_dir = Path::new(&source.dir);
537+
let is_type_dev = type_
538+
.as_ref()
539+
.map(|t| t.as_str() == "dev")
540+
.unwrap_or(false)
541+
.clone();
518542
match (build_dev_deps, type_) {
519543
(false, Some(type_)) if type_ == "dev" => (),
520-
_ => match read_folders(filter, package_dir, path_dir, recurse) {
544+
_ => match read_folders(filter, package_dir, path_dir, recurse, is_type_dev) {
521545
Ok(files) => map.extend(files),
522546

523547
Err(_e) => log::error!(
@@ -596,7 +620,7 @@ pub fn make(
596620
show_progress: bool,
597621
build_dev_deps: bool,
598622
) -> Result<AHashMap<String, Package>> {
599-
let map = read_packages(root_folder, workspace_root, show_progress)?;
623+
let map = read_packages(root_folder, workspace_root, show_progress, build_dev_deps)?;
600624

601625
/* Once we have the deduplicated packages, we can add the source files for each - to minimize
602626
* the IO */
@@ -720,6 +744,8 @@ pub fn parse_packages(build_state: &mut BuildState) {
720744
compile_dirty: false,
721745
last_compiled_cmt: None,
722746
last_compiled_cmi: None,
747+
// Not sure if this is correct
748+
is_type_dev: false,
723749
},
724750
);
725751
});
@@ -772,6 +798,7 @@ pub fn parse_packages(build_state: &mut BuildState) {
772798
compile_dirty: true,
773799
last_compiled_cmt: None,
774800
last_compiled_cmi: None,
801+
is_type_dev: metadata.is_type_dev,
775802
});
776803
} else {
777804
// remove last character of string: resi -> res, rei -> re, mli -> ml
@@ -833,6 +860,7 @@ pub fn parse_packages(build_state: &mut BuildState) {
833860
compile_dirty: true,
834861
last_compiled_cmt: None,
835862
last_compiled_cmi: None,
863+
is_type_dev: metadata.is_type_dev,
836864
});
837865
}
838866
}

rewatch/src/cli.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -214,9 +214,6 @@ pub enum Command {
214214
/// Path to a rescript file (.res or .resi)
215215
#[command()]
216216
path: String,
217-
218-
#[command(flatten)]
219-
dev: DevArg,
220217
},
221218
/// Use the legacy build system.
222219
///

0 commit comments

Comments
 (0)