<commit_before>#lang racket/base

(require syntax/modcode
         (for-syntax racket/base))

(provide enter!)

(define-syntax (enter! stx)
  (syntax-case stx ()
    [(enter! mod)
     (if (or (not (syntax-e #'mod))
             (module-path? (syntax->datum #'mod)))
         #'(do-enter! 'mod)
         (raise-syntax-error
          #f
          "not a valid module path, and not #f"
          stx
          #'mod))]
    [_ (raise-syntax-error
        #f
        "bad syntax; should be `(enter! <module-path-or-#f>)'"
        stx)]))

(define orig-namespace (current-namespace))

(define (do-enter! mod)
  (if mod
      (begin
        (enter-require mod)
        (let ([ns (module->namespace mod)])
          (current-namespace ns)
          (namespace-require 'racket/enter)))
      (current-namespace orig-namespace)))

(struct mod (name timestamp depends))

(define loaded (make-hash))

(define (enter-require mod)
  ;; Collect dependencies while loading:
  (parameterize ([current-load/use-compiled
                  (enter-load/use-compiled (current-load/use-compiled) #f)])
    (dynamic-require mod #f))
  ;; Reload anything that's not up to date:
  (check-latest mod))

(define (notify re? path)
  (fprintf (current-error-port) " [~aloading ~a]\n" (if re? "re-" "") path))

(define ((enter-load/use-compiled orig re?) path name)
  (if name
      ;; Module load:
      (let ([code (get-module-code path
                                   "compiled"
                                   (lambda (e)
                                     (parameterize ([compile-enforce-module-constants #f])
                                       (compile e)))
                                   (lambda (ext loader?)
                                     (load-extension ext)
                                     #f)
                                   #:notify (lambda (chosen)
                                              (notify re? chosen)))]
            [path (normal-case-path
                   (simplify-path
                    (path->complete-path path
                                         (or (current-load-relative-directory)
                                             (current-directory)))))])
        ;; Record module timestamp and dependencies:
        (let ([a-mod (mod name
                          (get-timestamp path)
                          (if code
                              (apply append
                                     (map cdr (module-compiled-imports code)))
                              null))])
          (hash-set! loaded path a-mod))
        ;; Evaluate the module:
        (eval code))
      ;; Not a module:
      (begin
        (notify re? path)
        (orig path name))))

(define (get-timestamp path)
  (file-or-directory-modify-seconds path #f (lambda () -inf.0)))

(define (check-latest mod)
  (let ([mpi (module-path-index-join mod #f)]
        [done (make-hash)])
    (let loop ([mpi mpi])<commit_msg>fix `enter!' reloading on a ".ss" path<commit_after>#lang racket/base

(require syntax/modcode
         (for-syntax racket/base))

(provide enter!)

(define-syntax (enter! stx)
  (syntax-case stx ()
    [(enter! mod)
     (if (or (not (syntax-e #'mod))
             (module-path? (syntax->datum #'mod)))
         #'(do-enter! 'mod)
         (raise-syntax-error
          #f
          "not a valid module path, and not #f"
          stx
          #'mod))]
    [_ (raise-syntax-error
        #f
        "bad syntax; should be `(enter! <module-path-or-#f>)'"
        stx)]))

(define orig-namespace (current-namespace))

(define (do-enter! mod)
  (if mod
      (begin
        (enter-require mod)
        (let ([ns (module->namespace mod)])
          (current-namespace ns)
          (namespace-require 'racket/enter)))
      (current-namespace orig-namespace)))

(struct mod (name timestamp depends))

(define loaded (make-hash))

(define (enter-require mod)
  ;; Collect dependencies while loading:
  (parameterize ([current-load/use-compiled
                  (enter-load/use-compiled (current-load/use-compiled) #f)])
    (dynamic-require mod #f))
  ;; Reload anything that's not up to date:
  (check-latest mod))

(define (notify re? path)
  (fprintf (current-error-port) " [~aloading ~a]\n" (if re? "re-" "") path))

(define ((enter-load/use-compiled orig re?) path name)
  (if name
      ;; Module load:
      (let ([code (get-module-code path
                                   "compiled"
                                   (lambda (e)
                                     (parameterize ([compile-enforce-module-constants #f])
                                       (compile e)))
                                   (lambda (ext loader?)
                                     (load-extension ext)
                                     #f)
                                   #:notify (lambda (chosen)
                                              (notify re? chosen)))]
            [path (normal-case-path
                   (simplify-path
                    (path->complete-path path
                                         (or (current-load-relative-directory)
                                             (current-directory)))))])
        ;; Record module timestamp and dependencies:
        (let ([a-mod (mod name
                          (get-timestamp path)
                          (if code
                              (apply append
                                     (map cdr (module-compiled-imports code)))
                              null))])
          (hash-set! loaded path a-mod))
        ;; Evaluate the module:
        (eval code))
      ;; Not a module:
      (begin
        (notify re? path)
        (orig path name))))

(define (get-timestamp path)
  (let ([v (file-or-directory-modify-seconds path #f (lambda () -inf.0))])
    (if (and (equal? v -inf.0)
             (regexp-match? #rx#"[.]rkt$" (path->bytes path)))
        (file-or-directory-modify-seconds (path-replace-suffix path #".ss")
                                          #f 
                                          (lambda () -inf.0))
        v)))

(define (check-latest mod)
  (let ([mpi (module-path-index-join mod #f)]
        [done (make-hash)])
    (let loop ([mpi mpi])
{'fraction_non_alphanumeric': 0.13127413127413126, 'fraction_numerical': 0.0006435006435006435, 'mean_word_length': 1.3567096285064442, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 36, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4118362', 'n_tokens_mistral': 1767, 'n_tokens_neox': 1668, 'n_words': 452}