Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support custom search directories #814

Merged
merged 7 commits into from
Sep 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions crates/bridge_core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -764,6 +764,11 @@ impl SecuritySettings {
pub fn allow_shell_escape(&self) -> bool {
!self.disable_insecures
}

/// Query whether we're allowed to specify extra paths to read files from.
pub fn allow_extra_search_paths(&self) -> bool {
!self.disable_insecures
}
}

impl Default for SecuritySettings {
Expand Down
8 changes: 7 additions & 1 deletion docs/src/v2cli/compile.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ tectonic -X compile myfile.tex

#### Usage Synopsis

<!-- Keep options alphabetized -->

```sh
tectonic -X compile # full form
[--bundle PATH] [-b PATH]
Expand Down Expand Up @@ -90,6 +92,8 @@ can trivially defeat this by explicitly clearing the environment variable.

The following are the available flags.

<!-- Keep alphabetized by full name: -->

| Short | Full | Explanation |
|:------|:--------------------------|:-----------------------------------------------------------------------------------------------|
| `-b` | `--bundle <PATH>` | Use this Zip-format bundle file to find resource files instead of the default |
Expand Down Expand Up @@ -119,11 +123,13 @@ The following are the available flags.
The following unstable options may be available. As the name aims to indicate,
the set of unstable options is subject to change at any time.

<!-- Keep alphabetized: -->

| Expression | Explanation |
|:-------------------------|:------------|
| `-Z help` | List all unstable options |
| `-Z continue-on-errors` | Keep compiling even when severe errors occur |
| `-Z min-crossrefs=<num>` | Equivalent to bibtex's `-min-crossrefs` flag. Default vaue: 2 |
| `-Z paper-size=<spec>` | Change the initial paper size. Default: `letter` |
| `-Z search-path=<path>` | Also look in `<path>` for files (unless `--untrusted` has been specified), like TEXINPUTS. Can be specified multiple times. |
| `-Z shell-escape` | Enable `\write18` (unless `--untrusted` has been specified) |

28 changes: 27 additions & 1 deletion src/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,9 @@ struct BridgeState {
/// The main filesystem backing for input files in the project.
filesystem: FilesystemIo,

/// Extra paths we search through for files.
extra_search_paths: Vec<FilesystemIo>,

/// Additional filesystem backing used if "shell escape" functionality is
/// activated. If None, we take that to mean that shell-escape is
/// disallowed. We have to use a persistent filesystem directory for this
Expand Down Expand Up @@ -447,6 +450,12 @@ macro_rules! bridgestate_ioprovider_cascade {
if let Some(ref mut p) = $self.shell_escape_work {
bridgestate_ioprovider_try!(p, $($inner)+);
}

// Extra search paths. This has higher priority than bundles but lower than current
// working dir to support the use case of overriding broken bundles (see issue #816).
for fsio in $self.extra_search_paths.iter_mut() {
bridgestate_ioprovider_try!(fsio, $($inner)+);
}
}

bridgestate_ioprovider_try!($self.bundle.as_ioprovider_mut(), $($inner)+);
Expand Down Expand Up @@ -1060,14 +1069,31 @@ impl ProcessingSessionBuilder {
None
};

let filesystem = FilesystemIo::new(&filesystem_root, false, true, self.hidden_input_paths);
// move this out of self to get around borrow checker issues
let hidden_input_paths = self.hidden_input_paths;

let extra_search_paths = if self.security.allow_extra_search_paths() {
self.unstables
.extra_search_paths
.iter()
.map(|p| FilesystemIo::new(p, false, false, hidden_input_paths.clone()))
.collect()
} else {
if !self.unstables.extra_search_paths.is_empty() {
tt_warning!(status, "Extra search path(s) ignored due to security");
}
Vec::new()
};

let filesystem = FilesystemIo::new(&filesystem_root, false, true, hidden_input_paths);

let mem = MemoryIo::new(true);

let bs = BridgeState {
primary_input: pio,
mem,
filesystem,
extra_search_paths,
shell_escape_work: None,
format_cache,
bundle,
Expand Down
34 changes: 24 additions & 10 deletions src/unstable_opts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@
//! to be reliable or very polished. In particular, many of these prevent the build from being
//! reproducible.

use crate::errors::{Error, Result};
use crate::{
errmsg,
errors::{Error, Result},
};
use std::default::Default;
use std::path::PathBuf;
use std::str::FromStr;

const HELPMSG: &str = r#"Available unstable options:
Expand All @@ -19,6 +23,8 @@ const HELPMSG: &str = r#"Available unstable options:
-Z min-crossrefs=<num> Equivalent to bibtex's -min-crossrefs flag - "include after <num>
crossrefs" [default: 2]
-Z paper-size=<spec> Change the default paper size [default: letter]
-Z search-path=<path> Also look in <path> for files, like TEXINPUTS. Can be specified
multiple times.
-Z shell-escape Enable \write18
"#;

Expand All @@ -29,6 +35,7 @@ pub enum UnstableArg {
Help,
MinCrossrefs(i32),
PaperSize(String),
SearchPath(PathBuf),
ShellEscapeEnabled,
}

Expand All @@ -44,25 +51,30 @@ impl FromStr for UnstableArg {
// For structopt/clap, if you pass a value to a flag which doesn't accept one, it's
// silently ignored.

let require_value = |value_name| {
value.ok_or_else(|| {
errmsg!(
"'-Z {}=<{}>' requires a value but none was supplied",
arg,
value_name
)
})
};

match arg {
"help" => Ok(UnstableArg::Help),

"continue-on-errors" => Ok(UnstableArg::ContinueOnErrors),

"min-crossrefs" => value
.ok_or_else(|| {
"'-Z min-crossrefs <spec>' requires a value but none was supplied".into()
})
"min-crossrefs" => require_value("num")
.and_then(|s| {
FromStr::from_str(s).map_err(|e| format!("-Z min-crossrefs: {}", e).into())
})
.map(UnstableArg::MinCrossrefs),

"paper-size" => value
.ok_or_else(|| {
"'-Z paper-size <spec>' requires a value but none was supplied".into()
})
.map(|s| UnstableArg::PaperSize(s.to_string())),
"paper-size" => require_value("spec").map(|s| UnstableArg::PaperSize(s.to_string())),

"search-path" => require_value("path").map(|s| UnstableArg::SearchPath(s.into())),

"shell-escape" => Ok(UnstableArg::ShellEscapeEnabled),

Expand All @@ -77,6 +89,7 @@ pub struct UnstableOptions {
pub paper_size: Option<String>,
pub shell_escape: bool,
pub min_crossrefs: Option<i32>,
pub extra_search_paths: Vec<PathBuf>,
}

impl UnstableOptions {
Expand All @@ -97,6 +110,7 @@ impl UnstableOptions {
MinCrossrefs(num) => opts.min_crossrefs = Some(num),
PaperSize(size) => opts.paper_size = Some(size),
ShellEscapeEnabled => opts.shell_escape = true,
SearchPath(p) => opts.extra_search_paths.push(p),
}
}

Expand Down
38 changes: 38 additions & 0 deletions tests/executable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -794,3 +794,41 @@ fn shell_escape_env_override() {

error_or_panic(&output);
}

/// Test that include paths work
#[test]
fn extra_search_paths() {
let fmt_arg = get_plain_format_arg();
let tempdir = setup_and_copy_files(&["subdirectory/content/1.tex"]);

let output = run_tectonic_with_stdin(
tempdir.path(),
&[&fmt_arg, "-", "-Zsearch-path=subdirectory/content"],
"\\input 1.tex\n\\bye",
);
success_or_panic(&output);

let output = run_tectonic_with_stdin(
tempdir.path(),
&[
&fmt_arg,
"-",
"--hide=subdirectory/content/1.tex",
"-Zsearch-path=subdirectory/content",
],
"\\input 1.tex\n\\bye",
);
error_or_panic(&output);

let output = run_tectonic_with_stdin(
tempdir.path(),
&[
&fmt_arg,
"-",
"-Zsearch-path=subdirectory/content",
"--untrusted",
],
"\\input 1.tex\n\\bye",
);
error_or_panic(&output);
}