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

cli: parse_opts should not actually spin off a repl #1393

Merged
merged 3 commits into from Mar 26, 2015

Conversation

richo
Copy link
Contributor

@richo richo commented Mar 11, 2015

While consuming pry programmatically in an internal project, I was surprised to discover that parse_options actually starts a pry session unless you mangle some internal state (clearing option_processors).

This patch breaks some APIs that I have no idea if there are meant to be guarantees around, so I can refactor if that's a problem, but in general causes parse_options to return the parsed options, which can then be passed to start.

@kyrylo
Copy link
Member

kyrylo commented Mar 12, 2015

Hi, @richo!

It would be nice if you could mention which APIs it breaks.

From local testing I have noticed a few oddities with this change:

  • I noticed that pry -e 123 produces an unwanted newline in history (that is, execute pry -e 123 and press )
  • when you define Pry.config.exec_string in your ~/.pryrc, you can no longer use the -e switch as the config value takes precedence (this is a general Pry issue not related to your patch)
  • #add_option_processor is unused now

Although I haven't tested the patch thoroughly, I remember that this part of code isn't tested well and if I recall correctly, there are some gotchas with respect to it. /cc @banister

Maybe, if you explained the problem you stumbled upon, we could probably find a different solution, because currently the motivation behind the change is a bit unclear to me.

@richo
Copy link
Contributor Author

richo commented Mar 24, 2015

Sorry I missed this!

So the issue specifically is that calling Pry::CLI.parse_options actually drops you into a repl, and we wanted to embed pry (with option parsing) into an internal project.

I'm very happy to fix the newline bug, I think it should be fairly straightforward. I can also remove add_option_processor.

The only API it breaks, is the behaviour that previously Pry::CLI.parse_options dropped you into a repl and returned.. something. Which I think is probably not correct, but was definitely what it did before and I broke that. parse_options now parses options and returns them, which I think is outwardly more reasonable.

ping @kyrylo because I missed this for a couple of weeks to make sure you see this

@richo
Copy link
Contributor Author

richo commented Mar 26, 2015

Just having had another read, it appears that add_option_processor constituted a part of the public API. I have no idea how to determine if anyone is using it, but my intuition is to leave it alone, unless you'd prefer that I don't.

@kyrylo
Copy link
Member

kyrylo commented Mar 26, 2015

Yeah, good catch. This is how it looked like previously (4 years ago): https://github.com/pry/pry/blob/d438a2f0370bfa99d7914cf06bacd8d6572484fa/lib/pry/cli.rb

And this commit introduced the change: https://github.com/pry/pry/blob/6a799a7fcd198e4bb4205dde055fca346176b867/lib/pry/cli.rb

So basically, you want to revert the feature :)

Although I don't know why it was made so exactly, my logic for keeping things as they are is that it was probably made on purpose. Only @banister knows the real reason. So I see 2 ways of resolving this issue.

  1. @banister comments on this and he decides what to do.
  2. I close this issue, because we don't want to break anything yet :)

@richo
Copy link
Contributor Author

richo commented Mar 26, 2015

So I'd really like to do something about this sooner rather than later. We currently have a thing internally that looks like:

def self.start_shell!
  require 'pry'
  # This can be delurked once https://github.com/pry/pry/pull/1393 lands
  Pry::CLI.option_processors.clear

  Pry::CLI.parse_options
  NameSpace.pry(:quiet => true)
end

In order to respect ordinary pry options, and then drop into a repl attached to an internal object. I can add another method to do just the stuff we want, but the API will officially be confusing by that point.

Regardless, I'd rather not close the issue, even if the PR doesn't land because I think we can agree this is a bug in pry? eg, it violates the principle of least surprise pretty drastically.

@kyrylo
Copy link
Member

kyrylo commented Mar 26, 2015

The code you posted reveals the issue better now. I agree that it's a problem. I think @banister overlooked it. Sorry, I had a hard time remembering this code, because I haven't worked on it for over a year or so.

I'd be happy to merge this once the exec_string stuff is resolved.

@richo
Copy link
Contributor Author

richo commented Mar 26, 2015

Cool, that's really helpful. I just fixed the newline bug, I'll have a quick skim over the precedence bug.

This still deals correctly with the case where there is more than one -e
option passed
@richo
Copy link
Contributor Author

richo commented Mar 26, 2015

Ok, and now the config files are loaded before the CLI options are parsed. This yields the weird (maybe?) behavior that they're additive but it's not really clear to me what the intended behaviour is here, and it does ~what multiple -es does on the CLI so I'm going to punt it over the fence to you, please let me know if you think it should be different.

r? @kyrylo closes #670

kyrylo added a commit that referenced this pull request Mar 26, 2015
cli: parse_opts should not actually spin off a repl
@kyrylo kyrylo merged commit d70f065 into pry:master Mar 26, 2015
@kyrylo
Copy link
Member

kyrylo commented Mar 26, 2015

Thanks, good stuff! ✌️

@kyrylo
Copy link
Member

kyrylo commented Jun 18, 2015

Heya! I just discovered that this PR breaks the -r switch.

For example:

code/pry/repl[d70f065...]% pry -rrss      
[1] pry(main)> RSS
NameError: uninitialized constant RSS

Do you have time to investigate this? :)

@richo
Copy link
Contributor Author

richo commented Jun 18, 2015

I can have a look, although it definitely seems to work in our environment. Poking now.

@richo
Copy link
Contributor Author

richo commented Jun 18, 2015

Ah, it's an order of operations bug. I'll get you a PR today.

@richo
Copy link
Contributor Author

richo commented Jun 18, 2015

#1435 makes the bug go away, and seems fairly idiomatic to me

netbsd-srcmastr pushed a commit to NetBSD/pkgsrc that referenced this pull request Mar 14, 2018
pkgsrc change: add support for pkg_alternatives

### HEAD

#### Features

* Add Pry::Testable, an improved modular replacement for PryTestHelpers.
  **breaking change**.

See pull request [#1679](pry/pry#1679).

* Add a new category module: "Pry::Platform". Loosely related to #1668 below.

See pull request [#1670](pry/pry#1670)

* Add `mac_osx?` and `linux?` utility functions to Pry::Helpers::BaseHelpers.

See pull request [#1668](pry/pry#1668).

* Add utility functions for drawing colorised text on a colorised background.

See pull request [#1673](pry/pry#1673).

#### Bug fixes

* Fix a case of infinite recursion in `Pry::Method::WeirdMethodLocator#find_method_in_superclass`
  that users of the [Hanami](http://hanamirb.org/) web framework experienced and
  reported since 2015.

See pull request [#1639](pry/pry#1689).

* Fix a bug where Method objects were not returned for setters inherited
  from a default (Pry::Config::Default). Eg, this is no longer an error:

      pry(main)> d = Pry::Config.from_hash({}, Pry::Config::Default.new)
      pry(main)> d.method(:exception_whitelist=) # Error

See pull request [#1688](pry/pry#1688).

* Do not capture unused Proc objects in Text helper methods `no_color` and `no_paging`,
  for performance reasons. Improve the documentation of both methods.

See pull request [#1691](pry/pry#1691).

* Fix `String#pp` output color.

See pull request [#1674](pry/pry#1674).

### 0.11.0

* Add alias 'whereami[?!]+' for 'whereami' command. ([#1597](pry/pry#1597))
* Improve Ruby 2.4 support ([#1611](pry/pry#1611)):
  * Deprecated constants are hidden from `ls` output by default, use the `-d` switch to see them.
  * Fix warnings that originate in Pry while using the repl.
* Improve completion speed in large applications. ([#1588](pry/pry#1588))
* Pry::ColorPrinter.pp: add `newline` argument and pass it on to PP. ([#1603](pry/pry#1603))
* Use `less` or system pager pager on MS Windows if it is available. ([#1512](pry/pry#1512))
* Add `Pry.configure` as an alternative to the current way of changing configuration options in `.pryrc` files. ([#1502](pry/pry#1502))
* Add `Pry::Config::Behavior#eager_load!` to add a possible workaround for issues like ([#1501](pry/pry#1501))
* Remove Slop as a runtime dependency by vendoring v3.4 as Pry::Slop.
  People can depend on Slop v4 and Pry at the same time without running into version conflicts. ([#1497](pry/pry#1497))
* Fix auto-indentation of code that uses a single-line rescue ([#1450](pry/pry#1450))
* Remove "Pry::Config#refresh", please use "Pry::Config#clear" instead.
* Defining a method called "ls" no longer breaks the "ls" command ([#1407](pry/pry#1407))
* Don't raise when directory permissions don't allow file expansion ([#1432](pry/pry#1432))
* Syntax highlight <tt> tags in documentation output.
* Add support for BasicObject subclasses who implement their own #inspect (#1341)
* Fix 'include RSpec::Matchers' at the top-level (#1277)
* Add 'gem-readme' command, prints the README file bundled with a rubygem
* Add 'gem-search' command, searches for a gem with the rubygems.org HTTP API
* Fixed bug in the `cat` command where it was impossible to use line numbers with files ([#1349](pry/pry#1349))
* Fixed uncaught Errno::EOPNOTSUPP exception when $stdout is a socket ([#1352](pry/pry#1352))
* Display a warning when you cd'ed inside a C object and executed 'show-source' without arguments ([#691](pry/pry#691))
* Make the stagger_output method more reliable by reusing possibly available Pry instance ([#1364](pry/pry#1364))
* Make the 'gem-install' message less confusing by removing backticks ([#1350](pry/pry#1350))
* Fixed error when Pry was trying to load incompatible versions of plugins ([#1312](pry/pry#1312))
* Fixed bug when `hist --clear` led to ArgumentError ([#1340](pry/pry#1340))
* Fixed the "uninitialized constant Pry::ObjectPath::StringScanner" exception during autocomplete ([#1330](pry/pry#1330))
* Secured usage of colours with special characters (RL_PROMPT_START_IGNORE and RL_PROMPT_END_IGNORE) in Pry::Helpers::Text ([#493](pry/pry#493 (comment)))
* Fixed regression with `pry -e` when it messes the terminal ([#1387](pry/pry#1387))
* Fixed regression with space prefixes of expressions ([#1369](pry/pry#1369))
* Introduced the new way to define hooks for commands (with `Pry.hooks.add_hook("{before,after}_commandName")`). The old way is deprecated, but still supported (with `Pry.commands.{before,after}_command`) ([#651](pry/pry#651))
* Removed old API's using `Pry::Hooks.from_hash` altogether
* Removed hints on Foreman support (see [this](ddollar/foreman#536))
* Fixed support for the tee command ([#1334](pry/pry#1334))
* Implemented support for CDPATH for ShellCommand ([#1433](pry/pry#1433), [#1434](pry/pry#1434))
* `Pry::CLI.parse_options` does not start Pry anymore ([#1393](pry/pry#1393))
* The gem uses CPU-less platforms for Windows now ([#1410](pry/pry#1410))
* Add `Pry::Config::Memoization` to make it easier to implement your own `Pry::Config::Default` class.([#1503](pry/pry#1503))
* Lazy load the config defaults for `Pry.config.history` and `Pry.config.gist`.
kyrylo added a commit that referenced this pull request Oct 21, 2018
Fixes #1761 (`pry -f` can no longer suppress the loading of .pryrc)

If I recall correctly, we split session loading in #1393 because of `Pry.start`.
This broke `-f`. I think we can get away with a simple reorder in #parse_options
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants