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

read-ns-form: rethrow FileNotFoundExceptions more informatively #368

Merged
merged 6 commits into from
Feb 10, 2022
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
3 changes: 3 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ executors:
- image: circleci/clojure:openjdk-11-lein-2.9.3-buster-node
environment:
LEIN_ROOT: "true" # we intended to run lein as root
LEIN_JVM_OPTS: -Dmranderson.internal.no-parallelism=true
JVM_OPTS: -Xmx3200m --illegal-access=deny # forbid reflective access (this flag doesn't exist for JDK8 or JDK17+)
<<: *defaults

Expand All @@ -47,6 +48,7 @@ executors:
- image: circleci/clojure:openjdk-16-lein-2.9.5-buster-node
environment:
LEIN_ROOT: "true" # we intended to run lein as root
LEIN_JVM_OPTS: -Dmranderson.internal.no-parallelism=true
JVM_OPTS: -Xmx3200m --illegal-access=deny # forbid reflective access (this flag doesn't exist for JDK8 or JDK17+)
<<: *defaults

Expand All @@ -55,6 +57,7 @@ executors:
- image: circleci/clojure:openjdk-17-lein-2.9.5-buster-node
environment:
LEIN_ROOT: "true" # we intended to run lein as root
LEIN_JVM_OPTS: -Dmranderson.internal.no-parallelism=true
JVM_OPTS: -Xmx3200m
<<: *defaults

Expand Down
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

## Unreleased

## 3.3.2

* [#173](https://github.com/clojure-emacs/refactor-nrepl/issues/173): `rename-file-or-dir`: rename more kinds of constructs in dependent namespaces: namespace-qualified maps, fully-qualified functions, metadata.
* [#194](https://github.com/clojure-emacs/refactor-nrepl/issues/194): Don't prune `require` forms if they are needed for a given `import` to work.
* [#142](https://github.com/clojure-emacs/refactor-nrepl/issues/142): `read-ns-form`: report more informatively when a non-existing file is being processed.

## 3.3.1

Expand Down
24 changes: 5 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[![CircleCI](https://circleci.com/gh/clojure-emacs/refactor-nrepl/tree/master.svg?style=svg)](https://circleci.com/gh/clojure-emacs/refactor-nrepl/tree/master)
[![Clojars Project](https://img.shields.io/clojars/v/refactor-nrepl/refactor-nrepl.svg)](https://clojars.org/refactor-nrepl/refactor-nrepl)
[![Dependencies Status](https://versions.deps.co/clojure-emacs/refactor-nrepl/status.svg)](https://versions.deps.co/clojure-emacs/refactor-nrepl)
[![cljdoc badge](https://cljdoc.org/badge/refactor-nrepl/refactor-nrepl)](https://cljdoc.org/d/refactor-nrepl/refactor-nrepl/CURRENT)
[![downloads badge](https://versions.deps.co/refactor-nrepl/refactor-nrepl/downloads.svg)](https://clojars.org/refactor-nrepl/refactor-nrepl)
Expand All @@ -25,25 +26,10 @@ Be aware that this isn't the case if you connect to an already running REPL proc
Add the following, either in your project's `project.clj`, or in the `:user` profile found at `~/.lein/profiles.clj`:

```clojure
:plugins [[refactor-nrepl "3.3.1"]
:plugins [[refactor-nrepl "3.3.2"]
[cider/cider-nrepl "0.25.9"]]
```

### Adding the middleware via Boot

Add the following in `~/.boot/profile.boot`:

```clojure
(require 'boot.repl)

(swap! boot.repl/*default-dependencies* conj
'[refactor-nrepl "3.3.1"]
'[cider/cider-nrepl "0.25.9"])

(swap! boot.repl/*default-middleware* conj
'refactor-nrepl.middleware/wrap-refactor)
```

### Embedded nREPL

You may want launch your own nREPL server with CIDER and refactor-nrepl in it. You'll be able to [`cider-connect`](https://github.com/clojure-emacs/cider/blob/6a17686799b7ef97bc15fa041016421e5c875bfb/cider.el#L1150) to said server.
Expand Down Expand Up @@ -371,12 +357,12 @@ If you want to use `mranderson` while developing locally with the REPL, the sour

When you want to release locally to the following:

PROJECT_VERSION=3.3.1 make install
PROJECT_VERSION=3.3.2 make install

And here's how to deploy to Clojars:

```bash
git tag -a v3.3.1 -m "3.3.1"
git tag -a v3.3.2 -m "3.3.2"
git push --tags
```

Expand All @@ -386,7 +372,7 @@ An extensive changelog is available [here](CHANGELOG.md).

## License

Copyright © 2013-2021 Benedek Fazekas, Magnar Sveen, Alex Baranosky, Lars Andersen, Bozhidar Batsov
Copyright © 2013-2022 Benedek Fazekas, Magnar Sveen, Alex Baranosky, Lars Andersen, Bozhidar Batsov

Distributed under the Eclipse Public License, the same as Clojure.

Expand Down
2 changes: 1 addition & 1 deletion project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
with-debug-bindings [[:inner 0]]
merge-meta [[:inner 0]]
try-if-let [[:block 1]]}}}]
:eastwood {:plugins [[jonase/eastwood "1.1.1"]]
:eastwood {:plugins [[jonase/eastwood "1.2.2"]]
:eastwood {;; :implicit-dependencies would fail spuriously when the CI matrix runs for Clojure < 1.10,
;; because :implicit-dependencies can only work for a certain corner case starting from 1.10.
:exclude-linters [:implicit-dependencies]
Expand Down
39 changes: 20 additions & 19 deletions src/refactor_nrepl/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
[refactor-nrepl.s-expressions :as sexp]
[refactor-nrepl.util :as util :refer [normalize-to-unix-path]])
(:import
(java.io File FileReader PushbackReader StringReader)))
(java.io File FileNotFoundException FileReader PushbackReader StringReader)))

;; Require our `fs` customizations before `fs` is loaded:
(require '[refactor-nrepl.fs])
Expand Down Expand Up @@ -152,28 +152,29 @@

(defn read-ns-form
([path]
(let [^String path-string (when (string? path)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DRYed out the duplication across the two arities of read-ns-form.

Took care of making them 100% equivalent. The key is here:

(if dialect
  {:read-cond :allow :features #{dialect}}
  nil)

The nil value makes readers/indexing-push-back-reader behave exactly as if it hadn't received this argument at all.

path)
^File path-file (when-not path-string
path)]
(with-open [file-reader (or (some-> path-string FileReader.)
(some-> path-file FileReader.))]
(try
(parse/read-ns-decl (readers/indexing-push-back-reader
(PushbackReader. file-reader)))
(catch Exception _ nil)))))
(read-ns-form nil path))
([dialect path]
(let [^String path-string (when (string? path)
path)
^File path-file (when-not path-string
path)]
(with-open [file-reader (or (some-> path-string FileReader.)
(some-> path-file FileReader.))]
(try
(parse/read-ns-decl (readers/indexing-push-back-reader
(PushbackReader. file-reader))
{:read-cond :allow :features #{dialect}})
(catch Exception _ nil))))))
path)
^File file (or path-file (File. path-string))]
(try
(with-open [file-reader (FileReader. file)]
(try
(parse/read-ns-decl (readers/indexing-push-back-reader
(PushbackReader. file-reader))
(if dialect
{:read-cond :allow :features #{dialect}}
nil))
(catch Exception _ nil)))
(catch FileNotFoundException e
(throw (ex-info (format "No such file: %s. This typically indicates an invalid request client-side."
(pr-str path))
{:path path
:dialect dialect
:file (str file)}
e)))))))

(defn cljc-extension? [^String path]
(.endsWith path ".cljc"))
Expand Down
23 changes: 13 additions & 10 deletions test/refactor_nrepl/core_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,19 @@
(assert-ignored-paths not-ignored false?)
(assert-ignored-paths (concat always-ignored sometimes-ignored) true?)))))

(deftest test-read-ns-form
(are [input expected] (testing input
(assert (-> input File. .exists))
(is (= expected
(sut/read-ns-form input)))
true)
"test-resources/readable_file_incorrect_aliases.clj" nil
"testproject/src/com/example/one.clj" '(ns com.example.one
(:require [com.example.two :as two :refer [foo]]
[com.example.four :as four]))))
(deftest read-ns-form-test
(let [valid-filename "testproject/src/com/example/one.clj"]
(is (= (sut/read-ns-form valid-filename)
(sut/read-ns-form :clj valid-filename)))
(are [input expected] (testing input
(assert (-> input File. .exists))
(is (= expected
(sut/read-ns-form input)))
true)
"test-resources/readable_file_incorrect_aliases.clj" nil
valid-filename '(ns com.example.one
(:require [com.example.two :as two :refer [foo]]
[com.example.four :as four])))))

(deftest source-files-with-clj-like-extension-test
(let [result (sut/source-files-with-clj-like-extension true)]
Expand Down