Skip to content

Commit c8e08be

Browse files
authored
Merge pr #144: More path improvements
This time they actually seems to work as intended.
2 parents 82628d8 + e90ad79 commit c8e08be

File tree

4 files changed

+34
-43
lines changed

4 files changed

+34
-43
lines changed

Diff for: CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,13 @@ project adheres to
99

1010
## Unreleased
1111

12+
* Use platform-dependent `path.join` for putting the local part after
13+
a base path, seems to make slash-separated strings ok as the joined
14+
part. (PR #144, issue #133)
1215
* Update sass-spec test suite to 2022-05-20.
1316

17+
Thanks to @fasterthanlime for reporting and testing.
18+
1419

1520
## Release 0.25.0
1621

Diff for: src/file_context.rs

+20-36
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::{Error, SourceFile, SourceName, SourcePos};
2+
use std::borrow::Cow;
23
use std::path::{Path, PathBuf};
34

45
/// A file context manages finding and loading files.
@@ -55,15 +56,8 @@ pub trait FileContext: Sized + std::fmt::Debug {
5556
];
5657
// Note: Should a "full stack" of bases be used here?
5758
// Or is this fine?
58-
let base = from.file_url();
59-
if let Some((path, mut file)) = base
60-
.rfind('/')
61-
.map(|p| base.split_at(p + 1).0)
62-
.map(|base| {
63-
do_find_file(self, &format!("{}{}", base, url), names)
64-
})
65-
.unwrap_or_else(|| do_find_file(self, url, names))?
66-
{
59+
let url = relative(from.file_url(), url);
60+
if let Some((path, mut file)) = do_find_file(self, &url, names)? {
6761
let source = SourceName::imported(path, from);
6862
Ok(Some(SourceFile::read(&mut file, source)?))
6963
} else {
@@ -89,15 +83,8 @@ pub trait FileContext: Sized + std::fmt::Debug {
8983
];
9084
// Note: Should a "full stack" of bases be used here?
9185
// Or is this fine?
92-
let base = from.file_url();
93-
if let Some((path, mut file)) = base
94-
.rfind('/')
95-
.map(|p| base.split_at(p + 1).0)
96-
.map(|base| {
97-
do_find_file(self, &format!("{}{}", base, url), names)
98-
})
99-
.unwrap_or_else(|| do_find_file(self, url, names))?
100-
{
86+
let url = relative(from.file_url(), url);
87+
if let Some((path, mut file)) = do_find_file(self, &url, names)? {
10188
let source = SourceName::used(path, from);
10289
Ok(Some(SourceFile::read(&mut file, source)?))
10390
} else {
@@ -119,7 +106,16 @@ pub trait FileContext: Sized + std::fmt::Debug {
119106
) -> Result<Option<(String, Self::File)>, Error>;
120107
}
121108

122-
/// Find a file for `@use`
109+
/// Make a url relative to a given base.
110+
fn relative<'a>(base: &str, url: &'a str) -> Cow<'a, str> {
111+
base.rfind('/')
112+
.map(|p| base.split_at(p + 1).0)
113+
.map(|base| format!("{}{}", base, url).into())
114+
.unwrap_or_else(|| url.into())
115+
}
116+
117+
/// Find a file in a given filecontext matching a url over a set of
118+
/// name rules.
123119
fn do_find_file<FC: FileContext>(
124120
ctx: &FC,
125121
url: &str,
@@ -194,28 +190,16 @@ impl FileContext for FsFileContext {
194190
&self,
195191
name: &str,
196192
) -> Result<Option<(String, Self::File)>, Error> {
197-
// TODO: Use rsplit_once when MSRV is 1.52 or above.
198-
let (parent, name) = if let Some(pos) = name.find('/') {
199-
(Some(&name[..pos + 1]), &name[pos + 1..])
200-
} else {
201-
(None, name)
202-
};
203193
if !name.is_empty() {
204194
for base in &self.path {
205-
use std::fmt::Write;
206-
let mut full = String::new();
207-
if !base.as_os_str().is_empty() {
208-
write!(&mut full, "{}/", base.display()).unwrap();
209-
}
210-
if let Some(parent) = parent {
211-
full.push_str(parent);
212-
}
213-
full.push_str(name);
195+
let full = base.join(name);
214196
if Path::new(&full).is_file() {
215197
tracing::debug!(?full, "opening file");
216198
return match Self::File::open(&full) {
217-
Ok(file) => Ok(Some((full, file))),
218-
Err(e) => Err(Error::Input(full, e)),
199+
Ok(file) => Ok(Some((name.to_string(), file))),
200+
Err(e) => {
201+
Err(Error::Input(full.display().to_string(), e))
202+
}
219203
};
220204
}
221205
tracing::trace!(?full, "Not found");
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
$color: purple;
1+
$color: black !default;
22

33
@function foo($v) {
44
@if $v > 0 {
@@ -7,9 +7,3 @@ $color: purple;
77
@return pink;
88
}
99
}
10-
11-
@mixin myem {
12-
em {
13-
color: foo(0);
14-
}
15-
}

Diff for: tests/basic/defs/_index.scss

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
$color: purple;
2+
@use foo with ($color: $color) as *;
3+
4+
@mixin myem {
5+
em {
6+
color: foo(0);
7+
}
8+
}

0 commit comments

Comments
 (0)