Auto merge of #23512 - cpud36:fix_incorrect_relative_path_in_webidl_plugin, r=Manishearth

Fix `#[webidl_must_inherit]` building when cwd is not servo crate root

Manifest dir should be more stable than current dir.

<!-- Please describe your changes on the following line: -->
I was trying to embed servo.

The directory layout of mine was the following:
```
/
*-servo
*-src
*-Cargo.toml
...
```

When I tried to build, cargo reported an error, in `#[webidl_must_inherit]` (Os error: 2).

After some research I have found that it failed to find `.webidl` files, because it used a path relative to `current_dir`.

I replaced current dir with manifest dir to make it more reliable.

---
<!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `___` with appropriate data: -->
- [x] `./mach build -d` does not report any errors
- [x] `./mach test-tidy` does not report any errors
- [ ] These changes fix #___ (Not applicable)

<!-- Either: -->
- [ ] There are tests for these changes OR
- [ ] These changes do not require tests because ___

I am unsure if these changes require tests. If they do, how should they be implemented?

<!-- Also, please make sure that "Allow edits from maintainers" checkbox is checked, so that we can help you if you get stuck somewhere along the way.-->

<!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. -->

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/23512)
<!-- Reviewable:end -->
This commit is contained in:
bors-servo 2019-06-04 23:02:34 -04:00 committed by GitHub
commit 7903104e6a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -66,9 +66,27 @@ fn get_ty_name(ty: &str) -> &str {
} }
} }
fn get_manifest_dir() -> io::Result<path::PathBuf> {
match env::var("CARGO_MANIFEST_DIR") {
Ok(var) => {
let mut dir = path::PathBuf::new();
dir.push(var);
Ok(dir)
},
Err(env::VarError::NotPresent) => Err(io::Error::new(
io::ErrorKind::NotFound,
"CARGO_MANIFEST_DIR environment variable was not found",
)),
Err(env::VarError::NotUnicode(_)) => Err(io::Error::new(
io::ErrorKind::InvalidData,
"CARGO_MANIFEST_DIR environment variable's contents are non valid UTF-8",
)),
}
}
fn get_webidl_path(struct_name: &str) -> io::Result<path::PathBuf> { fn get_webidl_path(struct_name: &str) -> io::Result<path::PathBuf> {
let mut dir = env::current_dir()?; let mut dir = get_manifest_dir()?;
dir.push("components/script/dom/webidls/"); dir.push("dom/webidls/");
dir.push(format!("{}.webidl", struct_name)); dir.push(format!("{}.webidl", struct_name));
Ok(dir) Ok(dir)