Skip to content

Commit 658d5a8

Browse files
Fix docs.rs build for rustpython-parser
docs.rs failed to build the documentation of the recently released rustpython-parser 0.2.0 because the build.rs script couldn't write the parser.rs file because docs.rs builds the documentation in a sandbox with a read-only filesystem. This commit fixes this by writing the parser.rs file to the cargo output directory instead, as recommended by the docs.rs documentation.[1] Fixes RustPython#4436. [1]: https://docs.rs/about/builds#read-only-directories
1 parent c7faae9 commit 658d5a8

File tree

3 files changed

+18
-15
lines changed

3 files changed

+18
-15
lines changed

.gitignore

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,3 @@ flamescope.json
1919

2020
extra_tests/snippets/resources
2121
extra_tests/not_impl.py
22-
23-
compiler/parser/python.rs

compiler/parser/build.rs

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
use std::fmt::Write as _;
22
use std::fs::File;
33
use std::io::{BufRead, BufReader, BufWriter, Write};
4-
use std::path::PathBuf;
4+
use std::path::{Path, PathBuf};
55
use tiny_keccak::{Hasher, Sha3};
66

77
fn main() -> anyhow::Result<()> {
88
const SOURCE: &str = "python.lalrpop";
9-
const TARGET: &str = "python.rs";
9+
let out_dir = PathBuf::from(std::env::var_os("OUT_DIR").unwrap());
1010

1111
println!("cargo:rerun-if-changed={SOURCE}");
1212

13-
try_lalrpop(SOURCE, TARGET)?;
14-
gen_phf();
13+
try_lalrpop(SOURCE, &out_dir.join("python.rs"))?;
14+
gen_phf(&out_dir);
1515

1616
Ok(())
1717
}
1818

19-
fn requires_lalrpop(source: &str, target: &str) -> Option<String> {
19+
fn requires_lalrpop(source: &str, target: &Path) -> Option<String> {
2020
let Ok(target) = File::open(target) else {
2121
return Some("python.rs doesn't exist. regenerate.".to_owned());
2222
};
@@ -68,16 +68,22 @@ fn requires_lalrpop(source: &str, target: &str) -> Option<String> {
6868
None
6969
}
7070

71-
fn try_lalrpop(source: &str, target: &str) -> anyhow::Result<()> {
71+
fn try_lalrpop(source: &str, target: &Path) -> anyhow::Result<()> {
7272
let Some(_message) = requires_lalrpop(source, target) else {
7373
return Ok(());
7474
};
7575

7676
#[cfg(feature = "lalrpop")]
77-
lalrpop::process_root().unwrap_or_else(|e| {
78-
println!("cargo:warning={_message}");
79-
panic!("running lalrpop failed. {e:?}");
80-
});
77+
// We are not using lalrpop::process_root() or Configuration::process_current_dir()
78+
// because of https://github.com/lalrpop/lalrpop/issues/699.
79+
lalrpop::Configuration::new()
80+
.use_cargo_dir_conventions()
81+
.set_in_dir(Path::new("."))
82+
.process()
83+
.unwrap_or_else(|e| {
84+
println!("cargo:warning={_message}");
85+
panic!("running lalrpop failed. {e:?}");
86+
});
8187

8288
#[cfg(not(feature = "lalrpop"))]
8389
{
@@ -98,8 +104,7 @@ fn sha_equal(expected_sha3_str: &str, actual_sha3: &[u8; 32]) -> bool {
98104
*actual_sha3 == expected_sha3
99105
}
100106

101-
fn gen_phf() {
102-
let out_dir = PathBuf::from(std::env::var_os("OUT_DIR").unwrap());
107+
fn gen_phf(out_dir: &Path) {
103108
let mut kwds = phf_codegen::Map::new();
104109
let kwds = kwds
105110
// Alphabetical keywords:

compiler/parser/src/python.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
#![allow(clippy::all)]
22
#![allow(unused)]
3-
include!("../python.rs");
3+
include!(concat!(env!("OUT_DIR"), "/python.rs"));

0 commit comments

Comments
 (0)