@@ -35,12 +35,20 @@ pub struct TsGoLintState {
35
35
36
36
impl TsGoLintState {
37
37
pub fn new ( cwd : & Path , config_store : ConfigStore ) -> Self {
38
- TsGoLintState {
39
- config_store,
40
- executable_path : try_find_tsgolint_executable ( cwd) . unwrap_or ( PathBuf :: from ( "tsgolint" ) ) ,
41
- cwd : cwd. to_path_buf ( ) ,
42
- silent : false ,
43
- }
38
+ let executable_path =
39
+ try_find_tsgolint_executable ( cwd) . unwrap_or ( PathBuf :: from ( "tsgolint" ) ) ;
40
+
41
+ TsGoLintState { config_store, executable_path, cwd : cwd. to_path_buf ( ) , silent : false }
42
+ }
43
+
44
+ /// Try to create a new TsGoLintState, returning an error if the executable cannot be found.
45
+ ///
46
+ /// # Errors
47
+ /// Returns an error if the tsgolint executable cannot be found.
48
+ pub fn try_new ( cwd : & Path , config_store : ConfigStore ) -> Result < Self , String > {
49
+ let executable_path = try_find_tsgolint_executable ( cwd) ?;
50
+
51
+ Ok ( TsGoLintState { config_store, executable_path, cwd : cwd. to_path_buf ( ) , silent : false } )
44
52
}
45
53
46
54
/// Set to `true` to skip file system reads.
@@ -710,15 +718,29 @@ fn parse_single_message(
710
718
/// Tries to find the `tsgolint` executable. In priority order, this will check:
711
719
/// 1. The `OXLINT_TSGOLINT_PATH` environment variable.
712
720
/// 2. The `tsgolint` binary in the current working directory's `node_modules/.bin` directory.
713
- pub fn try_find_tsgolint_executable ( cwd : & Path ) -> Option < PathBuf > {
721
+ ///
722
+ /// # Errors
723
+ /// Returns an error if `OXLINT_TSGOLINT_PATH` is set but does not exist or is not a file.
724
+ /// Returns an error if the tsgolint executable could not be resolve inside `node_modules/.bin`.
725
+ pub fn try_find_tsgolint_executable ( cwd : & Path ) -> Result < PathBuf , String > {
714
726
// Check the environment variable first
715
- if let Ok ( path ) = std:: env:: var ( "OXLINT_TSGOLINT_PATH" ) {
716
- let path = PathBuf :: from ( path ) ;
727
+ if let Ok ( path_str ) = std:: env:: var ( "OXLINT_TSGOLINT_PATH" ) {
728
+ let path = PathBuf :: from ( & path_str ) ;
717
729
if path. is_dir ( ) {
718
- return Some ( path. join ( "tsgolint" ) ) ;
719
- } else if path. is_file ( ) {
720
- return Some ( path) ;
730
+ let tsgolint_path = path. join ( "tsgolint" ) ;
731
+ if tsgolint_path. exists ( ) {
732
+ return Ok ( tsgolint_path) ;
733
+ }
734
+ return Err ( format ! (
735
+ "Failed to find tsgolint executable: OXLINT_TSGOLINT_PATH points to directory '{path_str}' but 'tsgolint' binary not found inside"
736
+ ) ) ;
737
+ }
738
+ if path. is_file ( ) {
739
+ return Ok ( path) ;
721
740
}
741
+ return Err ( format ! (
742
+ "Failed to find tsgolint executable: OXLINT_TSGOLINT_PATH points to '{path_str}' which does not exist"
743
+ ) ) ;
722
744
}
723
745
724
746
// executing a sub command in windows, needs a `cmd` or `ps1` extension.
@@ -730,7 +752,7 @@ pub fn try_find_tsgolint_executable(cwd: &Path) -> Option<PathBuf> {
730
752
loop {
731
753
let node_modules_bin = current_dir. join ( "node_modules" ) . join ( ".bin" ) . join ( file) ;
732
754
if node_modules_bin. exists ( ) {
733
- return Some ( node_modules_bin) ;
755
+ return Ok ( node_modules_bin) ;
734
756
}
735
757
736
758
// If we reach the root directory, stop searching
@@ -739,7 +761,7 @@ pub fn try_find_tsgolint_executable(cwd: &Path) -> Option<PathBuf> {
739
761
}
740
762
}
741
763
742
- None
764
+ Err ( "Failed to find tsgolint executable" . to_string ( ) )
743
765
}
744
766
745
767
#[ cfg( test) ]
0 commit comments