parser: modulesubpath defined with path macro needs to replace pathseperator to the default pathseperator

6c109ba
Opened by Jan Vandepitte at 2020-04-20 21:52:58

When trying out rustfmt on windows I got an error on my code that compiles. I have defined cross platform modules as such:

#[cfg(target_os = "linux")]
#[path = "proxyimpl/linux.rs"]
pub mod proxyimpl;
#[cfg(not(target_os = "linux"))]
#[path = "proxyimpl/nonlinux.rs"]
pub mod proxyimpl;

When running rustfmt on windows I get the following error

rustfmt .\main.rs
error: couldn't read "\\\\?\\C:\\Users\\<snip>\\Source\\irondragon\\src\\gpioaccess\\proxyimpl/linux.rs": The filename, directory name, or volume label syntax is incorrect. (os error 123)
  --> \\?\C:\Users\<snip>\Source\irondragon\src\gpioaccess\mod.rs:68:9
   |
68 | pub mod proxyimpl;
   |         ^^^^^^^^^

thread 'main' panicked at 'Box<Any>', src\libsyntax\parse\mod.rs:215:29
stack backtrace:
   0: <std::sync::condvar::WaitTimeoutResult as core::fmt::Debug>::fmt
   1: <std::time::SystemTimeError as core::fmt::Display>::fmt
   2: std::panicking::Location::column
   3: std::panicking::Location::column
   4: std::panicking::rust_panic_with_hook
   5: <syntax::ext::tt::macro_rules::TokenSet as core::fmt::Debug>::fmt
   6: syntax::parse::new_parser_from_tts
   7: syntax::parse::parser::Parser::default_submod_path
   8: syntax::parse::parser::Parser::parse_item
   9: syntax::parse::parser::Parser::parse_visibility
  10: syntax::parse::parser::Parser::default_submod_path
  11: syntax::parse::parser::Parser::parse_item
  12: syntax::parse::parser::Parser::parse_visibility
  13: syntax::parse::parser::Parser::parse_crate_mod
  14: <unknown>
  15: <unknown>
  16: <unknown>
  17: <unknown>
  18: <unknown>
  19: std::panicking::update_panic_count
  20: _rust_maybe_catch_panic
  21: std::rt::lang_start_internal
  22: <unknown>
  23: <unknown>
  24: BaseThreadInitThunk

Changing it to

#[cfg(target_os = "linux")]
#[path = "proxyimpl\\linux.rs"]
pub mod proxyimpl;
#[cfg(not(target_os = "linux"))]
#[path = "proxyimpl\\nonlinux.rs"]
pub mod proxyimpl;

I can run rustfmt on windows, but my travisci build (linux) fails.

  1. Just a guess, but have you tried the combo?

    #[cfg(target_os = "linux")] #[path = "proxyimpl/linux.rs"] pub mod proxyimpl;
    #[cfg(not(target_os = "linux"))] #[path = "proxyimpl\\nonlinux.rs"] pub mod proxyimpl;
    

    Alex Burka at 2018-02-21 18:40:05

  2. @durka if that is indeed the problem, the diagnostic should point it out:

    error: couldn't read "\\\\?\\C:\\Users\\<snip>\\Source\\irondragon\\src\\gpioaccess\\proxyimpl/linux.rs": The filename, directory name, or volume label syntax is incorrect. (os error 123)
      --> \\?\C:\Users\<snip>\Source\irondragon\src\gpioaccess\mod.rs:68:9
       |
    67 | #[path = "proxyimpl/nonlinux.rs"]
       | --------------------------------- verify the path separator being used matches the target platform (`/` vs `\`)
    68 | pub mod proxyimpl;
       |         ^^^^^^^^^
    

    Esteban Kuber at 2018-02-21 18:49:35

  3. @durka and @estebank : The problem is that rustfmt will try to parse both linux and non linux modules (but not compile it) since it's purpose is to format the code. The error I reported was on windows but it failed on the linux module ("\proxyimpl/linux.rs"). I think the parser should be able to parse code that is not compilable on a certain os.

    Jan Vandepitte at 2018-02-23 08:21:31

  4. Triage: @yenwel are you still seeing this behavior?

    Steve Klabnik at 2020-03-07 18:48:36