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

fix error with hot module replacement #264

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
e969116
fix error with hot module replacement
PatrykWalach Nov 12, 2024
25ba84c
update tests
PatrykWalach Nov 12, 2024
7d4628d
fix cleanup
PatrykWalach Nov 14, 2024
1ddc73b
don't use stale itemCleanupPairRef
PatrykWalach Nov 14, 2024
07de136
Rename TextSource -> RelativeTextSource and SourceFileName to Relativ…
rbalicki2 Jan 3, 2025
760b2c1
rename more structs to include Embedded in their name
rbalicki2 Jan 3, 2025
1169c86
Add AbsoluteEmbeddedLocation wrapper.
rbalicki2 Jan 3, 2025
bdd06db
add comment for guards added in `useEffects`
PatrykWalach Jan 3, 2025
e1a897d
fix error with hot module replacement
PatrykWalach Nov 12, 2024
ad1282a
update tests
PatrykWalach Nov 12, 2024
5bc585f
fix cleanup
PatrykWalach Nov 14, 2024
ef89d4d
don't use stale itemCleanupPairRef
PatrykWalach Nov 14, 2024
c8d6a76
add comment for guards added in `useEffects`
PatrykWalach Jan 3, 2025
f8e6e1b
Merge branch 'bandaid-error-on-hot-module-replacement' of github.com:…
PatrykWalach Jan 3, 2025
b18976d
run cargo fmt
rbalicki2 Jan 3, 2025
db10124
AbsoluteEmbeddedLocation doesnt need to implement Copy
rbalicki2 Jan 4, 2025
8a2ecd0
Create AbsoluteLocation struct
rbalicki2 Jan 4, 2025
b9ec7bd
std::fmt::Display the directive name directly, not the WithEmbededRel…
rbalicki2 Jan 4, 2025
0a63b51
remove unneeded impls
rbalicki2 Jan 4, 2025
7970dd3
Merge branch 'main' into bandaid-error-on-hot-module-replacement
PatrykWalach Jan 4, 2025
fbd0172
Merge branch 'main' into bandaid-error-on-hot-module-replacement
PatrykWalach Jan 6, 2025
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
103 changes: 58 additions & 45 deletions crates/common_lang_types/src/location.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
WithSpan,
};

/// A source, which consists of a filename, and an optional span
/// indicating the subset of the file which corresponds to the
/// source.
/// A source, which consists of a path from the config's project root
/// to the source file, and an optional span indicating the subset of
/// the file which corresponds to the source.
///
/// TODO consider whether to replace the span with an index,
/// as this will probably mean that sources are more reusable
Expand Down Expand Up @@ -68,40 +68,53 @@
}

#[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct EmbeddedLocation {
pub text_source: TextSource,
pub struct EmbeddedRelativeLocation {
pub text_source: RelativeTextSource,

Check failure on line 72 in crates/common_lang_types/src/location.rs

View workflow job for this annotation

GitHub Actions / Build json schema / Run build_json_schema

cannot find type `RelativeTextSource` in this scope

Check failure on line 72 in crates/common_lang_types/src/location.rs

View workflow job for this annotation

GitHub Actions / Build json schema / Run build_json_schema

cannot find type `RelativeTextSource` in this scope

Check failure on line 72 in crates/common_lang_types/src/location.rs

View workflow job for this annotation

GitHub Actions / cargo clippy

cannot find type `RelativeTextSource` in this scope

Check failure on line 72 in crates/common_lang_types/src/location.rs

View workflow job for this annotation

GitHub Actions / cargo clippy

cannot find type `RelativeTextSource` in this scope

Check failure on line 72 in crates/common_lang_types/src/location.rs

View workflow job for this annotation

GitHub Actions / Run cargo test (excluding relay tests)

cannot find type `RelativeTextSource` in this scope

Check failure on line 72 in crates/common_lang_types/src/location.rs

View workflow job for this annotation

GitHub Actions / Run cargo test (excluding relay tests)

cannot find type `RelativeTextSource` in this scope

Check failure on line 72 in crates/common_lang_types/src/location.rs

View workflow job for this annotation

GitHub Actions / Build compiler (x86_64-unknown-linux-musl) / Build compiler

cannot find type `RelativeTextSource` in this scope

Check failure on line 72 in crates/common_lang_types/src/location.rs

View workflow job for this annotation

GitHub Actions / Build compiler (x86_64-unknown-linux-musl) / Build compiler

cannot find type `RelativeTextSource` in this scope

Check failure on line 72 in crates/common_lang_types/src/location.rs

View workflow job for this annotation

GitHub Actions / Build compiler (aarch64-apple-darwin, macos-latest, isograph_cli, isograph_cli-macos-arm64) / Build compiler

cannot find type `RelativeTextSource` in this scope

Check failure on line 72 in crates/common_lang_types/src/location.rs

View workflow job for this annotation

GitHub Actions / Build compiler (aarch64-apple-darwin, macos-latest, isograph_cli, isograph_cli-macos-arm64) / Build compiler

cannot find type `RelativeTextSource` in this scope

Check failure on line 72 in crates/common_lang_types/src/location.rs

View workflow job for this annotation

GitHub Actions / Build compiler (x86_64-apple-darwin, macos-latest, isograph_cli, isograph_cli-macos-x64) / Build compiler

cannot find type `RelativeTextSource` in this scope

Check failure on line 72 in crates/common_lang_types/src/location.rs

View workflow job for this annotation

GitHub Actions / Build compiler (x86_64-apple-darwin, macos-latest, isograph_cli, isograph_cli-macos-x64) / Build compiler

cannot find type `RelativeTextSource` in this scope
/// The span is relative to the Source's span, not to the
/// entire source file.
pub span: Span,
}

impl std::fmt::Display for EmbeddedLocation {
/// What is happening here? EmbeddedRelativeLocation's only contain
/// a path that is relative to the Isograph config's project_root
/// field. Displaying the EmbeddedRelativeLocation involves reading the
/// file and displaying a subset of it.
///
/// The AbsoluteEmbeddedLocation struct knows how to turn that relative
/// path to an absolute path (i.e. it contains the absolute path to
/// the project_root) for use when reading the file.
#[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct AbsoluteEmbeddedLocation {
pub embedded_location: EmbeddedRelativeLocation,
}

impl std::fmt::Display for AbsoluteEmbeddedLocation {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let (file_path, read_out_text) = self.text_source.read_to_string();
let text_with_carats = text_with_carats(&read_out_text, self.span);
let (file_path, read_out_text) = self.embedded_location.text_source.read_to_string();
let text_with_carats = text_with_carats(&read_out_text, self.embedded_location.span);

write!(f, "{}\n{}", file_path, text_with_carats)
}
}

impl From<EmbeddedLocation> for Location {
fn from(value: EmbeddedLocation) -> Self {
impl From<EmbeddedRelativeLocation> for Location {
fn from(value: EmbeddedRelativeLocation) -> Self {
Location::Embedded(value)
}
}

#[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub enum Location {
Embedded(EmbeddedLocation),
Embedded(EmbeddedRelativeLocation),
Generated,
}

impl Location {
pub fn generated() -> Self {
Location::Generated
}
pub fn new(text_source: TextSource, span: Span) -> Self {
Location::Embedded(EmbeddedLocation::new(text_source, span))
pub fn new(text_source: RelativeTextSource, span: Span) -> Self {

Check failure on line 116 in crates/common_lang_types/src/location.rs

View workflow job for this annotation

GitHub Actions / Build json schema / Run build_json_schema

cannot find type `RelativeTextSource` in this scope

Check failure on line 116 in crates/common_lang_types/src/location.rs

View workflow job for this annotation

GitHub Actions / cargo clippy

cannot find type `RelativeTextSource` in this scope

Check failure on line 116 in crates/common_lang_types/src/location.rs

View workflow job for this annotation

GitHub Actions / Run cargo test (excluding relay tests)

cannot find type `RelativeTextSource` in this scope

Check failure on line 116 in crates/common_lang_types/src/location.rs

View workflow job for this annotation

GitHub Actions / Build compiler (x86_64-unknown-linux-musl) / Build compiler

cannot find type `RelativeTextSource` in this scope

Check failure on line 116 in crates/common_lang_types/src/location.rs

View workflow job for this annotation

GitHub Actions / Build compiler (aarch64-apple-darwin, macos-latest, isograph_cli, isograph_cli-macos-arm64) / Build compiler

cannot find type `RelativeTextSource` in this scope

Check failure on line 116 in crates/common_lang_types/src/location.rs

View workflow job for this annotation

GitHub Actions / Build compiler (x86_64-apple-darwin, macos-latest, isograph_cli, isograph_cli-macos-x64) / Build compiler

cannot find type `RelativeTextSource` in this scope
Location::Embedded(EmbeddedRelativeLocation::new(text_source, span))
}

pub fn span(&self) -> Option<Span> {
Expand All @@ -111,16 +124,23 @@
}
}
}
impl EmbeddedLocation {
pub fn new(text_source: TextSource, span: Span) -> Self {
EmbeddedLocation { text_source, span }
impl EmbeddedRelativeLocation {
pub fn new(text_source: RelativeTextSource, span: Span) -> Self {

Check failure on line 128 in crates/common_lang_types/src/location.rs

View workflow job for this annotation

GitHub Actions / Build json schema / Run build_json_schema

cannot find type `RelativeTextSource` in this scope

Check failure on line 128 in crates/common_lang_types/src/location.rs

View workflow job for this annotation

GitHub Actions / cargo clippy

cannot find type `RelativeTextSource` in this scope

Check failure on line 128 in crates/common_lang_types/src/location.rs

View workflow job for this annotation

GitHub Actions / Run cargo test (excluding relay tests)

cannot find type `RelativeTextSource` in this scope

Check failure on line 128 in crates/common_lang_types/src/location.rs

View workflow job for this annotation

GitHub Actions / Build compiler (x86_64-unknown-linux-musl) / Build compiler

cannot find type `RelativeTextSource` in this scope

Check failure on line 128 in crates/common_lang_types/src/location.rs

View workflow job for this annotation

GitHub Actions / Build compiler (aarch64-apple-darwin, macos-latest, isograph_cli, isograph_cli-macos-arm64) / Build compiler

cannot find type `RelativeTextSource` in this scope

Check failure on line 128 in crates/common_lang_types/src/location.rs

View workflow job for this annotation

GitHub Actions / Build compiler (x86_64-apple-darwin, macos-latest, isograph_cli, isograph_cli-macos-x64) / Build compiler

cannot find type `RelativeTextSource` in this scope
EmbeddedRelativeLocation { text_source, span }
}
}

impl fmt::Display for Location {
struct AbsoluteLocation {
pub location: Location,
}

impl fmt::Display for AbsoluteLocation {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Location::Embedded(e) => e.fmt(f),
match self.location {
Location::Embedded(embedded_location) => {
let wrapper = AbsoluteEmbeddedLocation { embedded_location };
wrapper.fmt(f)
}
Location::Generated => {
write!(f, "<generated>")
}
Expand All @@ -143,7 +163,10 @@

impl<T: fmt::Display> fmt::Display for WithLocation<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}\n{}", self.item, self.location)
let absolute_location = AbsoluteLocation {
location: self.location,
};
write!(f, "{}\n{}", self.item, absolute_location)
}
}

Expand All @@ -166,7 +189,7 @@
/// EmbeddedLocation or WithEmbeddedLocation
pub fn hack_to_with_span(self) -> WithSpan<T> {
let span = match self.location {
Location::Embedded(EmbeddedLocation { span, .. }) => span,
Location::Embedded(EmbeddedRelativeLocation { span, .. }) => span,
Location::Generated => Span::todo_generated(),
};
WithSpan {
Expand All @@ -177,47 +200,37 @@
}

#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq, Ord, PartialOrd)]
pub struct WithEmbeddedLocation<T> {
pub location: EmbeddedLocation,
pub struct WithEmbeddedRelativeLocation<T> {
pub location: EmbeddedRelativeLocation,
pub item: T,
}

impl<T: Error> Error for WithEmbeddedLocation<T> {
fn description(&self) -> &str {
#[allow(deprecated)]
self.item.description()
}
}

impl<T: fmt::Display> fmt::Display for WithEmbeddedLocation<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}\n{}", self.item, self.location)
}
}

impl<T> WithEmbeddedLocation<T> {
pub fn new(item: T, location: EmbeddedLocation) -> Self {
WithEmbeddedLocation { item, location }
impl<T> WithEmbeddedRelativeLocation<T> {
pub fn new(item: T, location: EmbeddedRelativeLocation) -> Self {
WithEmbeddedRelativeLocation { item, location }
}

pub fn map<U>(self, map: impl FnOnce(T) -> U) -> WithEmbeddedLocation<U> {
WithEmbeddedLocation::new(map(self.item), self.location)
pub fn map<U>(self, map: impl FnOnce(T) -> U) -> WithEmbeddedRelativeLocation<U> {
WithEmbeddedRelativeLocation::new(map(self.item), self.location)
}

pub fn and_then<U, E>(
self,
map: impl FnOnce(T) -> Result<U, E>,
) -> Result<WithEmbeddedLocation<U>, E> {
Ok(WithEmbeddedLocation::new(map(self.item)?, self.location))
) -> Result<WithEmbeddedRelativeLocation<U>, E> {
Ok(WithEmbeddedRelativeLocation::new(
map(self.item)?,
self.location,
))
}

pub fn into_with_location(self) -> WithLocation<T> {
self.into()
}
}

impl<T> From<WithEmbeddedLocation<T>> for WithLocation<T> {
fn from(value: WithEmbeddedLocation<T>) -> Self {
impl<T> From<WithEmbeddedRelativeLocation<T>> for WithLocation<T> {
fn from(value: WithEmbeddedRelativeLocation<T>) -> Self {
WithLocation {
location: Location::Embedded(value.location),
item: value.item,
Expand Down
17 changes: 13 additions & 4 deletions crates/common_lang_types/src/span.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use std::{fmt, ops::Range};

use crate::{EmbeddedLocation, Location, TextSource, WithEmbeddedLocation, WithLocation};
use crate::{
EmbeddedRelativeLocation, Location, RelativeTextSource, WithEmbeddedRelativeLocation,

Check failure on line 4 in crates/common_lang_types/src/span.rs

View workflow job for this annotation

GitHub Actions / Build json schema / Run build_json_schema

unresolved import `crate::RelativeTextSource`

Check failure on line 4 in crates/common_lang_types/src/span.rs

View workflow job for this annotation

GitHub Actions / cargo clippy

unresolved import `crate::RelativeTextSource`

Check failure on line 4 in crates/common_lang_types/src/span.rs

View workflow job for this annotation

GitHub Actions / Run cargo test (excluding relay tests)

unresolved import `crate::RelativeTextSource`

Check failure on line 4 in crates/common_lang_types/src/span.rs

View workflow job for this annotation

GitHub Actions / Build compiler (x86_64-unknown-linux-musl) / Build compiler

unresolved import `crate::RelativeTextSource`

Check failure on line 4 in crates/common_lang_types/src/span.rs

View workflow job for this annotation

GitHub Actions / Build compiler (aarch64-apple-darwin, macos-latest, isograph_cli, isograph_cli-macos-arm64) / Build compiler

unresolved import `crate::RelativeTextSource`

Check failure on line 4 in crates/common_lang_types/src/span.rs

View workflow job for this annotation

GitHub Actions / Build compiler (x86_64-apple-darwin, macos-latest, isograph_cli, isograph_cli-macos-x64) / Build compiler

unresolved import `crate::RelativeTextSource`
WithLocation,
};

// Invariant: end >= start
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
Expand Down Expand Up @@ -101,12 +104,18 @@
}
}

pub fn to_with_location(self, text_source: TextSource) -> WithLocation<T> {
pub fn to_with_location(self, text_source: RelativeTextSource) -> WithLocation<T> {
WithLocation::new(self.item, Location::new(text_source, self.span))
}

pub fn to_with_embedded_location(self, text_source: TextSource) -> WithEmbeddedLocation<T> {
WithEmbeddedLocation::new(self.item, EmbeddedLocation::new(text_source, self.span))
pub fn to_with_embedded_location(
self,
text_source: RelativeTextSource,
) -> WithEmbeddedRelativeLocation<T> {
WithEmbeddedRelativeLocation::new(
self.item,
EmbeddedRelativeLocation::new(text_source, self.span),
)
}
}

Expand Down
6 changes: 3 additions & 3 deletions crates/graphql_lang_types/src/directive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::fmt;

use super::{write::write_arguments, NameValuePair};
use crate::GraphQLConstantValue;
use common_lang_types::{DirectiveArgumentName, DirectiveName, WithEmbeddedLocation};
use common_lang_types::{DirectiveArgumentName, DirectiveName, WithEmbeddedRelativeLocation};
use intern::Lookup;
use serde::{
de::{self, value::SeqDeserializer, IntoDeserializer, MapAccess},
Expand All @@ -13,13 +13,13 @@ use thiserror::Error;
// TODO maybe this should be NameAndArguments and a field should be the same thing...?
#[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct GraphQLDirective<T> {
pub name: WithEmbeddedLocation<DirectiveName>,
pub name: WithEmbeddedRelativeLocation<DirectiveName>,
pub arguments: Vec<NameValuePair<DirectiveArgumentName, T>>,
}

impl<T: fmt::Display> fmt::Display for GraphQLDirective<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "@{}", self.name)?;
write!(f, "@{}", self.name.item)?;
write_arguments(f, &self.arguments)?;
Ok(())
}
Expand Down
Loading
Loading