Skip to content

Commit

Permalink
Add nonexistent variant to ModulePath
Browse files Browse the repository at this point in the history
Summary: See D68929960 for context. Having a variant for nonexistent path avoids the need of using a faked path.

Reviewed By: ndmitchell

Differential Revision: D68932048

fbshipit-source-id: 1c7e1e64e624e732423838118472c6fe22ebc513
  • Loading branch information
grievejia authored and facebook-github-bot committed Feb 1, 2025
1 parent 3316755 commit b467f8b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
16 changes: 16 additions & 0 deletions pyre2/pyre2/bin/module/module_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use std::sync::Arc;
use dupe::Dupe;

use crate::dunder;
use crate::module::module_name::ModuleName;

#[derive(Debug, Clone, Dupe, Copy, PartialEq, Eq, Hash)]
pub enum ModuleStyle {
Expand All @@ -33,6 +34,9 @@ enum ModulePathInner {
/// The module source comes from typeshed bundled with Pyre (which gets stored in-memory).
/// The path is relative to the root of the typeshed directory.
BundledTypeshed(PathBuf),
/// Representing the possibility that a module source doesn't exist.
/// The module name indicates the module that is looked up but not found.
NotFound(ModuleName),
}

fn is_path_init(path: &Path) -> bool {
Expand All @@ -58,6 +62,10 @@ impl ModulePath {
Self(Arc::new(ModulePathInner::BundledTypeshed(relative_path)))
}

pub fn not_found(module_name: ModuleName) -> Self {
Self(Arc::new(ModulePathInner::NotFound(module_name)))
}

pub fn display(&self) -> String {
match &*self.0 {
ModulePathInner::FileSystem(path) => path.display().to_string(),
Expand All @@ -67,13 +75,19 @@ impl ModulePath {
relative_path.display()
)
}
ModulePathInner::NotFound(module_name) => {
// This branch is not expected to be reachable since nonexistent module shouldn't have an errors,
// but lets make it clear this is a fake path if it ever happens to leak into any output.
format!("empty {module_name}")
}
}
}

pub fn is_init(&self) -> bool {
match &*self.0 {
ModulePathInner::FileSystem(path) => is_path_init(path),
ModulePathInner::BundledTypeshed(relative_path) => is_path_init(relative_path),
ModulePathInner::NotFound(_) => false,
}
}

Expand All @@ -82,6 +96,7 @@ impl ModulePath {
match &*self.0 {
ModulePathInner::FileSystem(path) => ModuleStyle::of_path(path),
ModulePathInner::BundledTypeshed(relative_path) => ModuleStyle::of_path(relative_path),
ModulePathInner::NotFound(_) => ModuleStyle::Executable,
}
}

Expand All @@ -93,6 +108,7 @@ impl ModulePath {
match &*self.0 {
ModulePathInner::FileSystem(path) => Some(path),
ModulePathInner::BundledTypeshed(_) => None,
ModulePathInner::NotFound(_) => None,
}
}
}
9 changes: 1 addition & 8 deletions pyre2/pyre2/bin/state/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,6 @@ pub struct LoadResultComponents {

static FAKE_MODULE: &str = "";

fn fake_path(module_name: ModuleName) -> PathBuf {
// The generated fake module shouldn't have an errors, but lets make it clear
// this is a fake path if it ever happens to leak into any output.
PathBuf::from(format!("/fake/{module_name}.py"))
}

impl LoadResult {
pub fn from_path(path: PathBuf) -> Self {
match fs_anyhow::read_to_string(&path) {
Expand All @@ -65,8 +59,7 @@ impl LoadResult {
import_error: None,
},
LoadResult::FailedToFind(err) => LoadResultComponents {
// TODO(grievejia): Properly model nonexistent paths
path: ModulePath::filesystem(fake_path(module_name)),
path: ModulePath::not_found(module_name),
code: FAKE_MODULE.to_owned(),
self_error: None,
import_error: Some(err),
Expand Down

0 comments on commit b467f8b

Please sign in to comment.